Skip to content

feat: allow explicit only event handlers - #7033

Open
benedikt-bartscher wants to merge 2 commits into
reflex-dev:mainfrom
benedikt-bartscher:disable-implicit-event-handlers
Open

feat: allow explicit only event handlers#7033
benedikt-bartscher wants to merge 2 commits into
reflex-dev:mainfrom
benedikt-bartscher:disable-implicit-event-handlers

Conversation

@benedikt-bartscher

@benedikt-bartscher benedikt-bartscher commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Closes #7032

Review in cubic

@greptile-apps

greptile-apps Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR adds an opt-in mode in which only methods decorated with @rx.event are registered as State event handlers. The latest changes also make State configuration resolution local to the active registration or in-progress configuration load.

  • Adds state_explicit_event_handlers configuration and documentation.
  • Marks decorated functions so State discovery can distinguish handlers from ordinary public helpers.
  • Replaces the process-global State flag cache with context-local configuration resolution.
  • Adds coverage for reload ordering, post-config State declarations, failure rollback, and separate registration contexts.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains; the current context-local resolution and in-progress config publication address all three previously reported configuration-selection defects.

Important Files Changed

Filename Overview
packages/reflex-base/src/reflex_base/config.py Resolves State flags from the in-progress config load or active RegistrationContext, addressing stale, ordering-dependent, and cross-context selection.
packages/reflex-base/src/reflex_base/event/init.py Marks functions processed by every supported event decorator form for explicit handler discovery.
reflex/state.py Filters user-defined State handlers by the event marker when explicit mode is enabled while retaining existing behavior for built-in states.
tests/units/test_state.py Covers explicit handler registration and the three previously reported configuration-selection failures.
tests/units/test_event.py Verifies that plain, configured, and background event decorators all apply the event marker.

Reviews (6): Last reviewed commit: "Merge remote-tracking branch 'upstream/m..." | Re-trigger Greptile

Comment thread packages/reflex-base/src/reflex_base/config.py Outdated
@codspeed-hq

codspeed-hq Bot commented Sep 2, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 32 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing benedikt-bartscher:disable-implicit-event-handlers (689dc48) with main (2459ded)2

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

  2. No successful run was found on main (f3aef84) during the generation of this report, so 2459ded was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@benedikt-bartscher
benedikt-bartscher marked this pull request as ready for review September 2, 2026 09:56
@benedikt-bartscher
benedikt-bartscher requested review from a team and Alek99 as code owners September 2, 2026 09:56
Comment thread packages/reflex-base/src/reflex_base/config.py Outdated

@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.

All reported issues were addressed across 8 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread packages/reflex-base/src/reflex_base/config.py Outdated
Comment thread tests/units/test_state.py
Comment thread news/7033.feature.md
Comment thread packages/reflex-base/src/reflex_base/config.py Outdated

@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.

All reported issues were addressed across 5 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread packages/reflex-base/src/reflex_base/config.py Outdated

@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.

All reported issues were addressed across 3 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Comment thread packages/reflex-base/src/reflex_base/config.py
@benedikt-bartscher
benedikt-bartscher force-pushed the disable-implicit-event-handlers branch from b2a522f to 945a53f Compare September 2, 2026 11:58

@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.

All reported issues were addressed across 8 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Comment thread packages/reflex-base/src/reflex_base/config.py

@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.

great, this gives us a lever and a chicken switch for eventually defaulting to requiring the decorator for event handlers.

need to ensure that reflex-enterprise's auth plugin and other integration points can still work with this new structure, but it seems like it would at first glance.


Adding the `@rx.event` decorator above the event handler is strongly recommended. This decorator enables proper static type checking, which ensures event handlers receive the correct number and types of arguments.

By default, every public method of a state (one whose name does not start with `_`) is treated as an event handler, whether or not it is decorated. To make `@rx.event` mandatory and keep undecorated public methods as plain Python helpers, enable `state_explicit_event_handlers` in `rxconfig.py`:

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.

Might also become default one day, following the Zen of Python

Explicit is better than implicit.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yeah, i wanted to go the same route as i did with state_auto_setters.

  1. introduce a flag to enable explicit mode - let feature stabilize and some projects adopt
  2. change the default to explicit
  3. drop implicit mode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

disable implicit @rx.event handlers

3 participants