Upcast str or int to annotated Enum type in event handlers#5855
Upcast str or int to annotated Enum type in event handlers#5855adhami3310 merged 1 commit intomainfrom
Conversation
If the argument of an event handler is annotated as a python Enum type, attempt to upcast the passed value to the Enum type.
There was a problem hiding this comment.
Greptile Overview
Summary
This PR adds automatic Enum type upcasting support to Reflex's event handler system. When an event handler parameter is annotated with a Python Enum type, the framework now automatically converts incoming raw values (strings or integers) to the specified Enum type.The implementation follows the existing pattern in Reflex's type conversion system, which already handles automatic upcasting for tuples, sets, dataclasses, and datetime objects. This change is located in the _process_args method within reflex/state.py, where it checks if the hinted argument type is an Enum subclass and attempts the conversion using the Enum constructor.
The feature includes proper error handling - if the value cannot be converted to the Enum (raising a ValueError), a descriptive error message is provided that includes the invalid value, parameter name, and expected Enum type. This maintains type safety while providing clear feedback to developers.
This enhancement eliminates the need for developers to write manual conversion logic in their event handlers, reducing boilerplate code and potential errors while maintaining consistency with the framework's existing type conversion capabilities.
Important Files Changed
Changed Files
| Filename | Score | Overview |
|---|---|---|
| reflex/state.py | 4/5 | Adds automatic Enum type upcasting to event handler argument processing with proper error handling |
Confidence score: 4/5
- This PR is safe to merge with minimal risk as it adds a well-contained feature with appropriate error handling
- Score reflects the straightforward implementation following existing patterns and inclusion of proper exception handling
- No files require special attention beyond standard code review practices
Sequence Diagram
sequenceDiagram
participant User
participant Frontend
participant Backend
participant EventHandler
participant TypeSystem
User->>Frontend: "Triggers event with value"
Frontend->>Backend: "Sends event payload"
Backend->>EventHandler: "Calls _process_event"
EventHandler->>TypeSystem: "Get type hints for handler"
TypeSystem-->>EventHandler: "Returns type annotations"
alt Value is Enum annotated
EventHandler->>TypeSystem: "Check if hinted_args is Enum subclass"
TypeSystem-->>EventHandler: "Confirms Enum type"
EventHandler->>EventHandler: "Attempt hinted_args(value)"
alt Enum conversion successful
EventHandler->>EventHandler: "Update payload[arg] with Enum value"
else Enum conversion fails
EventHandler->>EventHandler: "Raise ValueError with enum message"
end
end
EventHandler->>Backend: "Continue with processed payload"
Backend->>Frontend: "Returns state update"
Frontend->>User: "Updates UI with result"
1 file reviewed, no comments
CodSpeed Performance ReportMerging #5855 will not alter performanceComparing Summary
|
If the argument of an event handler is annotated as a python Enum type, attempt to upcast the passed value to the Enum type.