Skip to content

Commit 4afff3b

Browse files
authored
Upcast str or int to annotated Enum type in event handlers (#5855)
If the argument of an event handler is annotated as a python Enum type, attempt to upcast the passed value to the Enum type.
1 parent d24b856 commit 4afff3b

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

reflex/state.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import typing
1717
import warnings
1818
from collections.abc import AsyncIterator, Callable, Sequence
19+
from enum import Enum
1920
from hashlib import md5
2021
from importlib.util import find_spec
2122
from types import FunctionType
@@ -1880,6 +1881,12 @@ async def _process_event(
18801881
hinted_args is tuple or hinted_args is tuple
18811882
):
18821883
payload[arg] = tuple(value)
1884+
elif isinstance(hinted_args, type) and issubclass(hinted_args, Enum):
1885+
try:
1886+
payload[arg] = hinted_args(value)
1887+
except ValueError:
1888+
msg = f"Received an invalid enum value ({value}) for {arg} of type {hinted_args}"
1889+
raise ValueError(msg) from None
18831890
elif (
18841891
isinstance(value, str)
18851892
and (deserializer := _deserializers.get(hinted_args)) is not None

0 commit comments

Comments
 (0)