Debian Codegraph @

ninja/compatibility/util.py

raw symbols SCIP
1 import sys
2 from typing import Union
3
4 __all__ = ["UNION_TYPES", "get_annotations_from_namespace"]
5
6
7 # python3.10+ syntax of creating a union or optional type (with str | int)
8 # UNION_TYPES allows to check both universes if types are a union
9 try:
10 from types import UnionType
11
12 UNION_TYPES = (Union, UnionType)
13 except ImportError:
14 UNION_TYPES = (Union,)
15
16
17 # python3.14+ no longer puts __annotations__ in the class namespace dict
18 # during metaclass __new__; instead an __annotate__ function is used (PEP 749)
19 if sys.version_info >= (3, 14):
20 import annotationlib
21
22 def get_annotations_from_namespace(namespace: dict) -> dict:
23 ann = annotationlib.get_annotate_from_class_namespace(namespace)
24 if ann is not None:
25 return annotationlib.call_annotate_function(
26 ann, format=annotationlib.Format.VALUE
27 )
28 return namespace.get("__annotations__", {})
29
30 else:
31
32 def get_annotations_from_namespace(namespace: dict) -> dict:
33 return namespace.get("__annotations__", {})