Skip to content

Commit fc48a23

Browse files
committed
Fix schema generator for Python 3.14
Signed-off-by: xtex <[email protected]>
1 parent e43c144 commit fc48a23

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

generate_self_schema.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@
3030
TypesUnionType = TypingUnionType
3131
UnionType = TypingUnionType
3232

33+
try:
34+
import annotationlib
35+
36+
def get_annotations(obj):
37+
return {k: annotationlib.ForwardRef(v) for k, v in annotationlib.get_annotations(obj, format=annotationlib.Format.STRING).items()}
38+
39+
40+
except ImportError:
41+
def get_annotations(obj):
42+
return obj.__annotations__
43+
3344

3445
THIS_DIR = Path(__file__).parent
3546
SAVE_PATH = THIS_DIR / 'src' / 'self_schema.py'
@@ -121,7 +132,7 @@ def type_dict_schema( # noqa: C901
121132
required_keys = getattr(typed_dict, '__required_keys__', set())
122133
fields = {}
123134

124-
for field_name, field_type in typed_dict.__annotations__.items():
135+
for field_name, field_type in get_annotations(typed_dict).items():
125136
required = field_name in required_keys
126137
schema = None
127138
fr_arg = None
@@ -194,7 +205,9 @@ def all_literal_values(type_: type[core_schema.Literal]) -> list[any]:
194205

195206

196207
def eval_forward_ref(type_: Any) -> Any:
197-
if sys.version_info < (3, 12, 4):
208+
if sys.version_info >= (3, 14, 0):
209+
return type_.evaluate(globals=core_schema.__dict__)
210+
elif sys.version_info < (3, 12, 4):
198211
return type_._evaluate(core_schema.__dict__, None, recursive_guard=set())
199212
else:
200213
return type_._evaluate(core_schema.__dict__, None, type_params=set(), recursive_guard=set())
@@ -208,7 +221,7 @@ def main() -> None:
208221

209222
choices = {}
210223
for s in schema_union.__args__:
211-
type_ = s.__annotations__['type']
224+
type_ = get_annotations(s)['type']
212225
m = re.search(r"Literal\['(.+?)']", type_.__forward_arg__)
213226
assert m, f'Unknown schema type: {type_}'
214227
key = m.group(1)

0 commit comments

Comments
 (0)