Get all annotations including base classes. Builds a namespace from the Tortoise apps registry so that forward references (string annotations) to models defined in other files can be resolved by :func:`typing.get_type_hints`. :param cls: The model class we need annotations fro
(cls: type[Model], method: Callable | None = None)
| 8 | |
| 9 | |
| 10 | def get_annotations(cls: type[Model], method: Callable | None = None) -> dict[str, Any]: |
| 11 | """ |
| 12 | Get all annotations including base classes. |
| 13 | |
| 14 | Builds a namespace from the Tortoise apps registry so that forward references |
| 15 | (string annotations) to models defined in other files can be resolved by |
| 16 | :func:`typing.get_type_hints`. |
| 17 | |
| 18 | :param cls: The model class we need annotations from |
| 19 | :param method: If specified, we try to get the annotations for the callable |
| 20 | :return: The list of annotations |
| 21 | """ |
| 22 | localns: dict[str, Any] = {} |
| 23 | try: |
| 24 | from tortoise import Tortoise |
| 25 | |
| 26 | if Tortoise.apps: |
| 27 | for app_models in Tortoise.apps.values(): |
| 28 | localns.update(app_models) |
| 29 | except Exception: # nosec B110 |
| 30 | pass |
| 31 | try: |
| 32 | return get_type_hints(method or cls, localns=localns) |
| 33 | except Exception: |
| 34 | return getattr(method or cls, "__annotations__", {}) |
no test coverage detected
searching dependent graphs…