Repository navigation
Conversation
The decorator returned by `rx.event(...)` and the bare `rx.event(func)` form only accepted a plain function, so pyright rejected a second `@rx.event` applied to the `EventCallback` the first one produced, although the runtime merges both into one handler since reflex-dev#7807. Add overloads that take an `EventCallback[*P]` and return it unchanged, and drop the `# pyright: ignore` comments the repeated-decorator tests carried. Add assert_type examples to tests/type_checking/event.py. Closes reflex-dev#7847
[Low impact] This PR appears safe to merge. SummaryThis PR fixes the types of stacked
MaiAditya explicitly acknowledged that Reviews (1) · Last reviewed commit: "fix: type a repeated @rx.event as one ha..." · Reviewed by Greptile |
Merging this PR will degrade performance by 4.97%2 warnings
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing |
Closes #7847
The runtime has supported stacked
@rx.eventdecorators since #7807, but the type signatures only accepted a plain function. Pyright rejected the outer decorator because its argument was theEventCallbackproduced by the inner one.EventDecorator.__call__(whatrx.event(...)returns) gets an overload that takesEventCallback[*P]and returnsEventCallback[*P]. The overload for a plain function stays as it was.EventNamespace.__new__gets the matching overload, so a bare@rx.eventon top of@rx.event(...)also type-checks.S.handleris nowEventCallback[int](or whatever its arguments are) whether it has one decorator or two.# pyright: ignore [reportArgumentType]comments from the repeated-decorator tests intests/units/test_event.py. The ignore on the plain-function misuse case (event(stop_propagation=True)(summarize)) now reads[reportCallIssue, reportArgumentType], the same as theevent(ping)line above it, because the call now goes through overloads.tests/type_checking/event.pyhas newassert_typeexamples 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 sameEventCallback, soevent(OwnerState.handle)now type-checks even though it raisesTypeErrorat 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 intest_event.py, 7 on the newtype_checkingexamples). With the fix, pyright 1.1.411 reports 0 errors ontests/units/test_event.py tests/type_checkingand the event module under--pythonversion 3.11,3.12,3.14and the default.pyright reflex testsreports no new errors compared withmain.ty check --python-version 3.14 tests/type_checkingis 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:
Type of change
Changes To Core Features: