Repository navigation
fix(event): type rx.event handlers as EventCallback under mypy - #7715
Merged
Merged
Conversation
EventNamespace re-exports the event classes as plain class attributes (EventCallback = EventCallback). Inside the class body, the __new__ overloads' EventCallback[*P] return type resolves to that attribute, which mypy treats as a variable rather than a type. mypy then leaves the return type unresolved and falls back to EventNamespace, so every @rx.event handler is typed as EventNamespace and is not callable. Annotate the re-exports as TypeAlias so mypy resolves them as types. This also makes annotations such as rx.event.EventSpec valid under mypy. Runtime behavior is unchanged. Closes reflex-dev#7709 Signed-off-by: vjymisal0 <[email protected]>
Contributor
[Low impact] The PR appears safe to merge. SummaryAdds
Reviews (1) · Last reviewed commit: "fix(event): type rx.event handlers as Ev..." · Reviewed by Greptile |
Merging this PR will not alter performance1 warning
Comparing |
6 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Under mypy, every
@rx.eventhandler is typed asEventNamespace, so calling one directly fails with"EventNamespace" not callable [operator]. Pyright infersEventCallback[...], so CI doesn't catch it.The issue suggests mypy ignores the
__new__return type. The cause is a bit narrower. mypy does honor a__new__return type that isn't the class, but here it can't resolve that type at all.EventNamespacere-exports the event classes as plain class attributes (EventCallback = EventCallback). Inside the class body,EventCallback[*P]in the__new__overloads refers to that class attribute. mypy treats a class-levelX = Xas a variable, not a type alias. The return type stays unresolved (reveal_type(rx.event)shows-> EventCallback?[Any]), and mypy falls back toEventNamespace.Change:
EventNamespaceasTypeAlias(EventCallback: TypeAlias = EventCallback, and so on for the classes andTypeAliasTypes). mypy then resolves them as types.-> rx.event.EventSpec, now work under mypy too. That pattern appears in the docs and inreflex-components-internal. It used to fail withVariable "reflex_base.event.EventNamespace.EventSpec" is not valid as a type.Verification
mypy --strict(mypy 2.4.0):"EventNamespace" not callableons.bump(2)andawait s.load(), andreveal_type(S.bump)isEventNamespace.reveal_type(S.bump)isEventCallback[int].tests/type_checking/event.pyalready holds theassert_typecontract for handlers. Under mypy it had 30 errors onmainand has none with this change. The other files intests/type_checkinghave 7 mypy errors (fields.py,vars.py), the same onmainand unrelated. mypy isn't one of the checkers wired into pre-commit, so I didn't add a hook. I'm happy to add one scoped toevent.pyif you'd like a mypy guard.uv run pyright reflex tests packages/reflex-base: the error list is identical before and after (58 pre-existing, all Windows-only such asos.fork/signal.SIGKILLon my machine).ty check --python-version 3.14 tests/type_checking: passes.uv run pytest tests/units/reflex_base/event tests/units/test_event.py tests/units/test_state.py: 890 passed.packages/reflex-base/news/7709.bugfix.md.Closes #7709