|
11 | 11 | import json |
12 | 12 | import pickle |
13 | 13 | import sys |
| 14 | +import typing |
14 | 15 | import uuid |
15 | 16 | from abc import ABC, abstractmethod |
16 | 17 | from collections import defaultdict |
|
90 | 91 | ) |
91 | 92 | from reflex.utils.exec import is_testing_env |
92 | 93 | from reflex.utils.serializers import serializer |
93 | | -from reflex.utils.types import _isinstance, get_origin, override |
| 94 | +from reflex.utils.types import ( |
| 95 | + _isinstance, |
| 96 | + get_origin, |
| 97 | + is_union, |
| 98 | + override, |
| 99 | + value_inside_optional, |
| 100 | +) |
94 | 101 | from reflex.vars import VarData |
95 | 102 |
|
96 | 103 | if TYPE_CHECKING: |
@@ -1713,6 +1720,35 @@ async def _process_event( |
1713 | 1720 | # Get the function to process the event. |
1714 | 1721 | fn = functools.partial(handler.fn, state) |
1715 | 1722 |
|
| 1723 | + try: |
| 1724 | + type_hints = typing.get_type_hints(handler.fn) |
| 1725 | + except Exception: |
| 1726 | + type_hints = {} |
| 1727 | + |
| 1728 | + for arg, value in list(payload.items()): |
| 1729 | + hinted_args = type_hints.get(arg, Any) |
| 1730 | + if hinted_args is Any: |
| 1731 | + continue |
| 1732 | + if is_union(hinted_args): |
| 1733 | + if value is None: |
| 1734 | + continue |
| 1735 | + hinted_args = value_inside_optional(hinted_args) |
| 1736 | + if ( |
| 1737 | + isinstance(value, dict) |
| 1738 | + and inspect.isclass(hinted_args) |
| 1739 | + and ( |
| 1740 | + dataclasses.is_dataclass(hinted_args) |
| 1741 | + or issubclass(hinted_args, Base) |
| 1742 | + ) |
| 1743 | + ): |
| 1744 | + payload[arg] = hinted_args(**value) |
| 1745 | + if isinstance(value, list) and (hinted_args is set or hinted_args is Set): |
| 1746 | + payload[arg] = set(value) |
| 1747 | + if isinstance(value, list) and ( |
| 1748 | + hinted_args is tuple or hinted_args is Tuple |
| 1749 | + ): |
| 1750 | + payload[arg] = tuple(value) |
| 1751 | + |
1716 | 1752 | # Wrap the function in a try/except block. |
1717 | 1753 | try: |
1718 | 1754 | # Handle async functions. |
|
0 commit comments