Skip to content

Commit 97d4870

Browse files
committed
Upcast str or int to annotated Enum type in event handlers
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 b7230bd commit 97d4870

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 types import FunctionType
2122
from typing import TYPE_CHECKING, Any, BinaryIO, ClassVar, TypeVar, cast, get_type_hints
@@ -1882,6 +1883,12 @@ async def _process_event(
18821883
hinted_args is tuple or hinted_args is tuple
18831884
):
18841885
payload[arg] = tuple(value)
1886+
elif isinstance(hinted_args, type) and issubclass(hinted_args, Enum):
1887+
try:
1888+
payload[arg] = hinted_args(value)
1889+
except ValueError:
1890+
msg = f"Received an invalid enum value ({value}) for {arg} of type {hinted_args}"
1891+
raise ValueError(msg) from None
18851892
elif (
18861893
isinstance(value, str)
18871894
and (deserializer := _deserializers.get(hinted_args)) is not None

0 commit comments

Comments
 (0)