Skip to content

fix(event): type rx.event handlers as EventCallback under mypy - #7715

Merged
masenf merged 1 commit into
reflex-dev:mainfrom
vjymisal0:fix/rx-event-mypy-type
Nov 2, 2026
Merged

masenf merged 1 commit into
reflex-dev:mainfrom
vjymisal0:fix/rx-event-mypy-type

Conversation

@vjymisal0

@vjymisal0 vjymisal0 commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Under mypy, every @rx.event handler is typed as EventNamespace, so calling one directly fails with "EventNamespace" not callable [operator]. Pyright infers EventCallback[...], 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. EventNamespace re-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-level X = X as a variable, not a type alias. The return type stays unresolved (reveal_type(rx.event) shows -> EventCallback?[Any]), and mypy falls back to EventNamespace.

Change:

  • Annotate the type re-exports in EventNamespace as TypeAlias (EventCallback: TypeAlias = EventCallback, and so on for the classes and TypeAliasTypes). mypy then resolves them as types.
  • Side effect: annotations through the namespace, such as -> rx.event.EventSpec, now work under mypy too. That pattern appears in the docs and in reflex-components-internal. It used to fail with Variable "reflex_base.event.EventNamespace.EventSpec" is not valid as a type.
  • No runtime change: each attribute still holds the same object.

Verification

  • The reproduction from the issue under mypy --strict (mypy 2.4.0):
    • Before: "EventNamespace" not callable on s.bump(2) and await s.load(), and reveal_type(S.bump) is EventNamespace.
    • After: no errors, and reveal_type(S.bump) is EventCallback[int].
  • tests/type_checking/event.py already holds the assert_type contract for handlers. Under mypy it had 30 errors on main and has none with this change. The other files in tests/type_checking have 7 mypy errors (fields.py, vars.py), the same on main and 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 to event.py if 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 as os.fork / signal.SIGKILL on 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.
  • pre-commit (ruff, codespell, update-pyi-files, pyright, ty) passes on the changed files.
  • Added the news fragment packages/reflex-base/news/7709.bugfix.md.

Closes #7709

View guided diff

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]>
@vjymisal0
vjymisal0 requested a review from a team as a code owner November 2, 2026 04:56

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

View guided diff | Re-trigger cubic

@greptile-apps

greptile-apps Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Low impact] The PR appears safe to merge.

Summary

Adds TypeAlias annotations to the types exposed through EventNamespace so mypy can resolve decorated handlers and namespace-based type hints.

  • Mypy now recognizes event handlers and types through the event namespace.

Reviews (1) · Last reviewed commit: "fix(event): type rx.event handlers as Ev..." · Reviewed by Greptile

@codspeed

codspeed Bot commented Nov 2, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 20 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 148 untouched benchmarks


Comparing vjymisal0:fix/rx-event-mypy-type (7126fff) with main (aeea15a)

Open in CodSpeed

@masenf masenf added bug Something isn't working on deck PRs lined up to review / merge next labels Nov 2, 2026

@masenf masenf left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the patch 👍

@masenf
masenf merged commit be8fca0 into reflex-dev:main Nov 2, 2026
175 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working on deck PRs lined up to review / merge next

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@rx.event handlers are typed as EventNamespace, so calling them is "not callable"

2 participants