11"""Compatibility hacks and helpers."""
22
3+ import sys
4+ from collections .abc import Mapping
35from importlib .util import find_spec
46from typing import TYPE_CHECKING , Any
57
@@ -31,6 +33,27 @@ async def windows_hot_reload_lifespan_hack():
3133 pass
3234
3335
36+ def annotations_from_namespace (namespace : Mapping [str , Any ]) -> dict [str , Any ]:
37+ """Get the annotations from a class namespace.
38+
39+ Args:
40+ namespace: The class namespace.
41+
42+ Returns:
43+ The (forward-ref) annotations from the class namespace.
44+ """
45+ if sys .version_info >= (3 , 14 ) and "__annotations__" not in namespace :
46+ from annotationlib import (
47+ Format ,
48+ call_annotate_function ,
49+ get_annotate_from_class_namespace ,
50+ )
51+
52+ if annotate := get_annotate_from_class_namespace (namespace ):
53+ return call_annotate_function (annotate , format = Format .FORWARDREF )
54+ return namespace .get ("__annotations__" , {})
55+
56+
3457if find_spec ("pydantic" ) and find_spec ("pydantic.v1" ):
3558 from pydantic .v1 .main import ModelMetaclass
3659
@@ -49,8 +72,7 @@ def __new__(mcs, name: str, bases: tuple, namespace: dict, **kwargs):
4972 Returns:
5073 The created class.
5174 """
52- if (_anotate := namespace .get ("__annotate_func__" )) is not None :
53- namespace ["__annotations__" ] = _anotate (0 )
75+ namespace ["__annotations__" ] = annotations_from_namespace (namespace )
5476 return super ().__new__ (mcs , name , bases , namespace , ** kwargs )
5577else :
5678 ModelMetaclassLazyAnnotations = type # type: ignore[assignment]
@@ -85,12 +107,8 @@ def sqlmodel_get_annotations(class_dict: dict[str, Any]) -> dict[str, Any]:
85107 """
86108 from reflex .utils .types import resolve_annotations
87109
88- if (_annotate := class_dict .get ("__annotate_func__" )) is not None :
89- annotations = _annotate (0 )
90- else :
91- annotations = class_dict .get ("__annotations__" , {})
92110 return resolve_annotations ( # type: ignore[no-any-return]
93- annotations ,
111+ annotations_from_namespace ( class_dict ) ,
94112 class_dict .get ("__module__" ),
95113 )
96114
0 commit comments