Repository navigation
Type bare @rx.event handlers under pyright before Python 3.14 - #7810
Conversation
A second @rx.event on a handler crashed at class creation (FINDING-001 of the 0.11.0a1 pre-release testing, e.g. reflex-examples overkey): the decorator wrapped the inner EventHandler in another one, or failed its background check or frozen-dataclass setattr first. It now unwraps the handler and decorates its function again, silently: the markers accumulate on the function (background/supersedes if either decorator set them) and the outer decorator's event actions win where both set one. A repeated decorator on a decentralized handler re-registers it under the name it already has. A @staticmethod or @classmethod stacked above @rx.event on a state returned an EventSpec instead of calling the function (FINDING-016). The state (and a state copying it from a mixin) now restores the plain method and warns like @rx.event below them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
[Medium impact] The PR appears safe to merge; no new actionable issues were found. SummaryRestores argument types for bare
No new actionable issues were found. Checks in this review were source-based; tests were not run. Reviews (4) · Last reviewed commit: "Re-import the generic rx.event re-export..." · Reviewed by Greptile |
There was a problem hiding this comment.
All reported issues were addressed across 10 files
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
Merging this PR will degrade performance by 6.76%2 warnings
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing |
….event Review follow-up. An outer decorator's explicit False or 0 action now overrides the inner decorator's value (e.g. prevent_default=False over prevent_default=True); a single @rx.event whose actions are all False or 0 still carries none, so its compiled output is unchanged. A second @rx.event on a decentralized handler of a module named like __main__ no longer trips the private-name check on the "_main___" name its registration gave it. The tests now cover a mixin's repeated handler and count each staticmethod/classmethod warning. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
pyright on Python 3.11-3.13 typed every bare @rx.event handler as EventCallback[*tuple[Unknown, ...]] (FINDING-007 of the 0.11.0a1 pre-release testing): the EventNamespace re-export of EventCallback shadowed the module class in the __new__ overload's return annotation, and 3.14's deferred annotations hid it. The annotations now use a module-level alias. pyright also read every explicit TypeAlias re-export of a generic in the class body as already specialized, rejecting annotations such as rx.event.EventCallback[int] on every Python version. The generic re-exports (EventCallback, LambdaEventCallback, IdentityEventReturn, EventType, LAMBDA_OR_STATE, IndividualEventType) are plain assignments again, as in 0.10.1; the non-generic ones keep TypeAlias so mypy still accepts rx.event.EventSpec, and mypy still types handlers as EventCallback through the module-level alias. pre-commit now runs pyright over tests/type_checking under Python 3.11 and 3.14 regardless of the running interpreter. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
f5bd795 to
d34b69c
Compare
|
On CodSpeed's Code path. Neither PR touches state access. #7807's runtime changes run only at class or decoration time:
This PR's own commit only changes type-checker annotations: re-export assignments and a module-level Local numbers.
The best minimums are within ±2% of main. The medians spread widely within each column, so they cannot show a trend. 🤖 Generated with Claude Code https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi Generated by Claude Code |
…runs Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
A plain class-body assignment is a variable to mypy, so rx.event.EventCallback[int] and the other generic re-exports failed with valid-type. A class-body `from reflex_base.event import X as X` binds the class itself: pyright, mypy and ty all accept the subscripted annotation. EventCallback in the class body is then the class again, so the module-level _EventCallback workaround goes away.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
* Merge a repeated @rx.event; restore staticmethod/classmethod above it A second @rx.event on a handler crashed at class creation (FINDING-001 of the 0.11.0a1 pre-release testing, e.g. reflex-examples overkey): the decorator wrapped the inner EventHandler in another one, or failed its background check or frozen-dataclass setattr first. It now unwraps the handler and decorates its function again, silently: the markers accumulate on the function (background/supersedes if either decorator set them) and the outer decorator's event actions win where both set one. A repeated decorator on a decentralized handler re-registers it under the name it already has. A @staticmethod or @classmethod stacked above @rx.event on a state returned an EventSpec instead of calling the function (FINDING-016). The state (and a state copying it from a mixin) now restores the plain method and warns like @rx.event below them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi * Honor an outer False/0 action and __main__ handlers on a repeated @rx.event Review follow-up. An outer decorator's explicit False or 0 action now overrides the inner decorator's value (e.g. prevent_default=False over prevent_default=True); a single @rx.event whose actions are all False or 0 still carries none, so its compiled output is unchanged. A second @rx.event on a decentralized handler of a module named like __main__ no longer trips the private-name check on the "_main___" name its registration gave it. The tests now cover a mixin's repeated handler and count each staticmethod/classmethod warning. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi * Type bare @rx.event handlers under pyright before Python 3.14 pyright on Python 3.11-3.13 typed every bare @rx.event handler as EventCallback[*tuple[Unknown, ...]] (FINDING-007 of the 0.11.0a1 pre-release testing): the EventNamespace re-export of EventCallback shadowed the module class in the __new__ overload's return annotation, and 3.14's deferred annotations hid it. The annotations now use a module-level alias. pyright also read every explicit TypeAlias re-export of a generic in the class body as already specialized, rejecting annotations such as rx.event.EventCallback[int] on every Python version. The generic re-exports (EventCallback, LambdaEventCallback, IdentityEventReturn, EventType, LAMBDA_OR_STATE, IndividualEventType) are plain assignments again, as in 0.10.1; the non-generic ones keep TypeAlias so mypy still accepts rx.event.EventSpec, and mypy still types handlers as EventCallback through the module-level alias. pre-commit now runs pyright over tests/type_checking under Python 3.11 and 3.14 regardless of the running interpreter. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi * Re-import the generic rx.event re-exports so mypy accepts them A plain class-body assignment is a variable to mypy, so rx.event.EventCallback[int] and the other generic re-exports failed with valid-type. A class-body `from reflex_base.event import X as X` binds the class itself: pyright, mypy and ty all accept the subscripted annotation. EventCallback in the class body is then the class again, so the module-level _EventCallback workaround goes away. --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Farhan <www.mfarvirus@gmail.com> (cherry picked from commit b95fbf5)
…maining commits Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dxjadq3qkhKnqZR2b9WBFi
All Submissions:
Type of change
Changes To Core Features:
This is a release fix-up for FINDING-007 (MEDIUM, regression) from the reflex 0.11.0a1 pre-release testing (artifacts on branch
claude/vibrant-pasteur-3921g3,prerelease-testing/runs/2026-11-11-v0.11.0a1/).Defect
@rx.eventhandler asEventCallback[*tuple[Unknown, ...]]. On 0.10.1 it wasEventCallback[int]. Wrong-type calls are no longer reported, and a# pyright: strictapp goes from 4 to 18 errors.@rx.event()with parentheses is not affected.EventCallback: TypeAlias = EventCallbackinsideclass EventNamespace, which shadows the class in the__new__overload's-> EventCallback[*P].rx.event.EventCallback[int]as "already specialized". It does this for every explicitTypeAliasre-export of a generic in that class body, sorx.event.LambdaEventCallback[...],rx.event.IdentityEventReturn[...]andrx.event.EventType[...]fail too. All of them were clean on 0.10.1.Fix
from reflex_base.event import X as X:EventCallback,LambdaEventCallback,IdentityEventReturn,EventType,LAMBDA_OR_STATEandIndividualEventType. An import binds the class itself rather than declaring an alias, so pyright, mypy and ty all acceptrx.event.EventCallback[int]. In the class bodyEventCallbackis the class again, so the overload's-> EventCallback[*P]types handlers asEventCallback[int]under pyright 3.11–3.14 and mypy, fix(event): type rx.event handlers as EventCallback under mypy #7715's goal.TypeAlias, so mypy still acceptsrx.event.EventSpec, fix(event): type rx.event handlers as EventCallback under mypy #7715's other goal. Runtime behaviour does not change: eachrx.event.Xis thereflex_base.event.Xobject..pre-commit-config.yamlgets two hooks,pyright-type-checking-3.11andpyright-type-checking-3.14. They runpyright --pythonversion <v> tests/type_checkingalways, whatever the running interpreter. The pre-commit workflow already runspre-commit run --all-files, so no workflow, gate or ruleset change is needed.tests/type_checking/README.mdis updated to match.tests/type_checking/event.pygainsassert_typechecks for annotations made through the namespace (event.EventCallback[str],event.LambdaEventCallback[str],event.IdentityEventReturn[int],event.EventType[str],event.EventSpec).Test plan
Failing before (source fix reverted, new tests and hooks kept):
assert_types on_HandlerState.*and_State.set_namereceivedEventCallback[*tuple[Unknown, ...]].After the fix:
--pythonversion3.11, 3.12, 3.13 and 3.14 overtests/type_checkingreports 0 errors.typasses too.S.bareasEventCallback[int]and reportsS.bare("wrong")as an error.rx.event.EventCallback[int]is accepted as an annotation.S.bareasEventCallback[int], catchesS.bare("wrong"), and acceptsrx.event.EventSpecas an annotation.rx.event, andtests/type_checking/event.pyis clean under it. With plain class-body assignments it reported 7 errors there (valid-typeandassert-type).uv run pyright reflex tests: 0 errors.ruff checkandruff formatare clean.uv run pytest tests/units --cov --no-cov-on-fail --cov-report=: 15643 passed, 66 skipped, coverage 81.43%.tests/units/reflex_bench/drivers/test_browser.pywas excluded because the sandbox has no Chromium for it.Notes
TypeAliasto a bare generic as already specialized, at module scope too;TypeAliasin a class body);TypeAliasTypeunderTYPE_CHECKINGtrips a mypy bug withTypeVarTuple.An import is not an alias declaration, so neither rule applies. It is the explicit re-export idiom (
from x import Y as Y) that libraries such as FastAPI use at module scope.Generated by Claude Code