Skip to content

Upcast str or int to annotated Enum type in event handlers#5855

Merged
adhami3310 merged 1 commit intomainfrom
masenf/upcast-enum
Oct 8, 2025
Merged

Upcast str or int to annotated Enum type in event handlers#5855
adhami3310 merged 1 commit intomainfrom
masenf/upcast-enum

Conversation

@masenf
Copy link
Collaborator

@masenf masenf commented Oct 6, 2025

If the argument of an event handler is annotated as a python Enum type, attempt to upcast the passed value to the Enum type.

If the argument of an event handler is annotated as a python Enum type, attempt
to upcast the passed value to the Enum type.
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 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"
Loading

1 file reviewed, no comments

Edit Code Review Agent Settings | Greptile

@codspeed-hq
Copy link

codspeed-hq bot commented Oct 6, 2025

CodSpeed Performance Report

Merging #5855 will not alter performance

Comparing masenf/upcast-enum (97d4870) with main (b7230bd)

Summary

✅ 8 untouched

@adhami3310 adhami3310 merged commit 4afff3b into main Oct 8, 2025
51 of 53 checks passed
@adhami3310 adhami3310 deleted the masenf/upcast-enum branch October 8, 2025 23:35
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