|
13 | 13 | from dateutil import parser as dt_parser |
14 | 14 | from json import JSONDecodeError |
15 | 15 |
|
| 16 | +# 3.14 introduced lazy annotation loading, we must use the 'annotationlib' to inspect annotations. |
| 17 | +if not (sys.version_info.major == 3 and sys.version_info.minor < 14): |
| 18 | + from annotationlib import get_annotations, Format as annot_format |
| 19 | + |
16 | 20 | _enum_t = type(enum.Enum) |
17 | 21 |
|
18 | 22 | # Support OrderedDict for Python versions 3.6 or below. |
@@ -45,8 +49,8 @@ def _get_annot_cls(annots: dict, key: str, ignore_builtins = False) -> typing.Li |
45 | 49 | cls_ = cls_.__args__[0] |
46 | 50 | # Check if typing annotation class is a Union type. |
47 | 51 | # Try to find the right object class in the Union types list, ignore 'builtin' types. |
48 | | - if '__args__' in cls_.__dict__ and isinstance(cls_.__dict__['__args__'], (list, tuple)): |
49 | | - for cls_item in cls_.__dict__['__args__']: |
| 52 | + if hasattr(cls_, '__args__') and isinstance(cls_.__args__, (list, tuple)): |
| 53 | + for cls_item in cls_.__args__: |
50 | 54 | # Try to find the right object class in the Union types list, ignore 'builtin' types. |
51 | 55 | if issubclass(type(cls_item), object) and not isinstance(cls_item, typing.TypeVar): |
52 | 56 | if ignore_builtins and cls_item.__module__ == 'builtins': |
@@ -75,10 +79,12 @@ def _collect_annotations(self, cls_: object): |
75 | 79 | continue |
76 | 80 | result = self._collect_annotations(base) |
77 | 81 | annots.update(result) |
78 | | - |
79 | | - if hasattr(cls_, '__annotations__'): |
80 | | - annots.update(cls_.__annotations__) |
81 | | - |
| 82 | + # 3.14 introduced breaking changes to annotation inspection due to lazy annotation loading. |
| 83 | + if sys.version_info.major == 3 and sys.version_info.minor < 14: |
| 84 | + if hasattr(cls_, '__annotations__'): |
| 85 | + annots.update(cls_.__annotations__) |
| 86 | + else: |
| 87 | + annots.update(get_annotations(cls_, format=annot_format.VALUE)) |
82 | 88 | return annots |
83 | 89 |
|
84 | 90 | @staticmethod |
|
0 commit comments