fix: type a repeated @rx.event as one handler

MA
1d ago
+54-9

Closes #7847

The runtime has supported stacked @rx.event decorators since #7807, but the type signatures only accepted a plain function. Pyright rejected the outer decorator because its argument was the EventCallback produced by the inner one.

  • EventDecorator.__call__ (what rx.event(...) returns) gets an overload that takes EventCallback[*P] and returns EventCallback[*P]. The overload for a plain function stays as it was.
  • EventNamespace.__new__ gets the matching overload, so a bare @rx.event on top of @rx.event(...) also type-checks.
  • S.handler is now EventCallback[int] (or whatever its arguments are) whether it has one decorator or two.
  • I removed the # pyright: ignore [reportArgumentType] comments from the repeated-decorator tests in tests/units/test_event.py. The ignore on the plain-function misuse case (event(stop_propagation=True)(summarize)) now reads [reportCallIssue, reportArgumentType], the same as the event(ping) line above it, because the call now goes through overloads.
  • tests/type_checking/event.py has new assert_type examples for three stackings: @event(...) over @event(...), bare over configured, and configured over bare.

Limitation: the type of a handler read from a state class (OwnerState.handle) is the same EventCallback, so event(OwnerState.handle) now type-checks even though it raises TypeError at runtime. That test's ignore comment no longer suppresses anything, so it is replaced with a comment saying so.

Tests: on main, pyright reports 14 errors on the edited tests (7 on the stacked decorators in test_event.py, 7 on the new type_checking examples). With the fix, pyright 1.1.411 reports 0 errors on tests/units/test_event.py tests/type_checking and the event module under --pythonversion 3.11, 3.12, 3.14 and the default. pyright reflex tests reports no new errors compared with main. ty check --python-version 3.14 tests/type_checking is clean. mypy agrees on the same examples (not part of CI). pytest tests/units/test_event.py: 157 passed. Ruff check and format are clean.

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?