| 1 |
import sys |
| 2 |
from typing import Union |
| 3 |
|
| 4 |
__all__ = ["UNION_TYPES", "get_annotations_from_namespace"] |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
try: |
| 10 |
from types import UnionType |
| 11 |
|
| 12 |
UNION_TYPES = (Union, UnionType) |
| 13 |
except ImportError: |
| 14 |
UNION_TYPES = (Union,) |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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__", {}) |