Retrieves annotations from a Python object. In python >=3.14 this is a thin wrapper around the `annotationlib.get_annotations` function with the added convenience to automatically infer the type for non module, class, function or customly annotated objects.
(obj: Any)
| 29 | |
| 30 | |
| 31 | def get_annotations(obj: Any) -> Dict[str, Any]: |
| 32 | """ |
| 33 | Retrieves annotations from a Python object. |
| 34 | |
| 35 | In python >=3.14 this is a thin wrapper around the `annotationlib.get_annotations` function |
| 36 | with the added convenience to automatically infer the type for non module, class, function |
| 37 | or customly annotated objects. |
| 38 | """ |
| 39 | if annotationlib: |
| 40 | has_annotations = hasattr(obj, "__annotations__") or hasattr(obj, "__annotate__") |
| 41 | if not isinstance(obj, type) and not ismodule(obj) and not callable(obj) and not has_annotations: |
| 42 | obj = type(obj) |
| 43 | return annotationlib.get_annotations(obj) |
| 44 | try: |
| 45 | return obj.__annotations__ |
| 46 | except AttributeError: |
| 47 | return {} |