|
7 | 7 | import dataclasses |
8 | 8 | import functools |
9 | 9 | import inspect |
10 | | -import sys |
11 | 10 | import typing |
12 | 11 | from abc import ABC, ABCMeta, abstractmethod |
13 | 12 | from collections.abc import Callable, Iterator, Mapping, Sequence |
|
20 | 19 | Annotated, |
21 | 20 | Any, |
22 | 21 | ClassVar, |
23 | | - ForwardRef, |
24 | 22 | Generic, |
25 | 23 | TypeVar, |
26 | | - _eval_type, # pyright: ignore [reportAttributeAccessIssue] |
27 | 24 | cast, |
28 | 25 | get_args, |
29 | 26 | get_origin, |
|
74 | 71 | from reflex.vars.object import ObjectVar |
75 | 72 | from reflex.vars.sequence import LiteralArrayVar, LiteralStringVar, StringVar |
76 | 73 |
|
77 | | - |
78 | | -def resolve_annotations( |
79 | | - raw_annotations: Mapping[str, type[Any]], module_name: str | None |
80 | | -) -> dict[str, type[Any]]: |
81 | | - """Partially taken from typing.get_type_hints. |
82 | | -
|
83 | | - Resolve string or ForwardRef annotations into type objects if possible. |
84 | | -
|
85 | | - Args: |
86 | | - raw_annotations: The raw annotations to resolve. |
87 | | - module_name: The name of the module. |
88 | | -
|
89 | | - Returns: |
90 | | - The resolved annotations. |
91 | | - """ |
92 | | - module = sys.modules.get(module_name, None) if module_name is not None else None |
93 | | - |
94 | | - base_globals: dict[str, Any] | None = ( |
95 | | - module.__dict__ if module is not None else None |
96 | | - ) |
97 | | - |
98 | | - annotations = {} |
99 | | - for name, value in raw_annotations.items(): |
100 | | - if isinstance(value, str): |
101 | | - if sys.version_info == (3, 10, 0): |
102 | | - value = ForwardRef(value, is_argument=False) |
103 | | - else: |
104 | | - value = ForwardRef(value, is_argument=False, is_class=True) |
105 | | - try: |
106 | | - if sys.version_info >= (3, 13): |
107 | | - value = _eval_type(value, base_globals, None, type_params=()) |
108 | | - else: |
109 | | - value = _eval_type(value, base_globals, None) |
110 | | - except NameError: |
111 | | - # this is ok, it can be fixed with update_forward_refs |
112 | | - pass |
113 | | - annotations[name] = value |
114 | | - return annotations |
115 | | - |
116 | | - |
117 | 74 | FIELD_TYPE = TypeVar("FIELD_TYPE") |
118 | 75 |
|
119 | 76 |
|
@@ -228,7 +185,7 @@ def __new__(cls, name: str, bases: tuple[type], namespace: dict[str, Any]) -> ty |
228 | 185 | # Add the field to the class |
229 | 186 | inherited_fields: dict[str, ComponentField] = {} |
230 | 187 | own_fields: dict[str, ComponentField] = {} |
231 | | - resolved_annotations = resolve_annotations( |
| 188 | + resolved_annotations = types.resolve_annotations( |
232 | 189 | namespace.get("__annotations__", {}), namespace["__module__"] |
233 | 190 | ) |
234 | 191 |
|
|
0 commit comments