Skip to content

PropsBase converts EventHandler-annotated props to EventChain#5765

Merged
adhami3310 merged 4 commits intomainfrom
masenf/props-base-event-handler
Sep 12, 2025
Merged

PropsBase converts EventHandler-annotated props to EventChain#5765
adhami3310 merged 4 commits intomainfrom
masenf/props-base-event-handler

Conversation

@masenf
Copy link
Collaborator

@masenf masenf commented Sep 5, 2025

Typing issue remains in that EventCallback/EventType cannot be assigned to a field annotated as EventHandler without pyi processing... not sure if we want to merge without support for this.

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

Greptile Summary

This PR refactors event handling in the Reflex framework by centralizing EventHandler-to-EventChain conversion at the PropsBase level. The changes extract the logic for processing EventHandler-annotated fields from the Component class into a new shared utility function args_specs_from_fields in the event module, and introduce automatic conversion in PropsBase initialization.

Key Changes:

  • New utility function: args_specs_from_fields in event.py extracts argument specifications from fields annotated with EventHandler types, checking for metadata or defaulting to no-args specs
  • Component simplification: The get_event_triggers method in component.py now uses the new utility function instead of inline logic
  • PropsBase enhancement: The __init__ method now automatically converts EventHandler-annotated fields to EventChain objects using the extracted arg specs
  • Test coverage: New tests verify that State event methods assigned to EventHandler fields are properly converted to EventChain instances

This architectural change moves event handling logic from the component layer to the props layer, creating consistency across all components that inherit from PropsBase. The refactoring enables developers to assign State methods directly to EventHandler-annotated fields (like on_click=FooState.handle_click), while the system automatically converts them to the required EventChain format internally. The utility function is also added as a static method to EventNamespace for broader accessibility.

PR Description Notes:

  • Contains a typo: "pyi processing" should likely be "type processing" or reference to .pyi stub files

Confidence score: 4/5

  • This PR introduces significant architectural changes to event handling but appears well-implemented with proper testing
  • Score reflects the acknowledged typing limitation where EventCallback/EventType cannot be assigned to EventHandler fields without workarounds
  • Pay close attention to reflex/components/props.py and the event handling conversion logic in PropsBase

4 files reviewed, 1 comment

Edit Code Review Bot Settings | Greptile

Comment on lines +271 to +283
# Convert EventHandler to EventChain
args_specs = args_specs_from_fields(self.get_fields())
for handler_name, args_spec in args_specs.items():
if (handler := getattr(self, handler_name, None)) is not None:
setattr(
self,
handler_name,
EventChain.create(
value=handler,
args_spec=args_spec,
key=handler_name,
),
)
Copy link
Contributor

Choose a reason for hiding this comment

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

style: The EventChain conversion logic executes for every PropsBase instantiation even when no EventHandler fields exist. Consider adding a quick check to skip this block if args_specs is empty for better performance.

@codspeed-hq
Copy link

codspeed-hq bot commented Sep 5, 2025

CodSpeed Performance Report

Merging #5765 will not alter performance

Comparing masenf/props-base-event-handler (ea12ce6) with main (f0b2075)

Summary

✅ 8 untouched

adhami3310
adhami3310 previously approved these changes Sep 11, 2025
@adhami3310 adhami3310 dismissed their stale review September 11, 2025 21:56

the unit tests aren't passing, but otherwise looks fine

@adhami3310 adhami3310 merged commit 247c021 into main Sep 12, 2025
41 checks passed
@adhami3310 adhami3310 deleted the masenf/props-base-event-handler branch September 12, 2025 15:37
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.

2 participants