From fe11aa557a46ae0742c56c97b1296d4aea61ee3e Mon Sep 17 00:00:00 2001 From: WizzyGeek <51919967+WizzyGeek@users.noreply.github.com> Date: Fri, 10 May 2024 12:13:56 +0530 Subject: [PATCH] fix: Make get_annotations evaluate annotations in default scope (#1609) * fix: Make get_annotations evaluate annotations in default scope fixes #1552 * misc: update changelog and fix lint --- CHANGELOG.rst | 1 + CONTRIBUTORS.rst | 1 + tortoise/contrib/pydantic/utils.py | 5 +---- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index fd0d38230..6dbe6757d 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -24,6 +24,7 @@ Fixed - Fix `DatetimeField` use '__year' report `'int' object has no attribute 'utcoffset'`. (#1575) - Fix `bulk_update` when using custom fields. (#1564) - Fix `optional` parameter in `pydantic_model_creator` does not work for pydantic v2. (#1551) +- Fix `get_annotations` now evaluates annotations in the default scope instead of the app namespace. (#1552) 0.20.1 ------ diff --git a/CONTRIBUTORS.rst b/CONTRIBUTORS.rst index 05e5cf0f3..69d1e326e 100644 --- a/CONTRIBUTORS.rst +++ b/CONTRIBUTORS.rst @@ -55,6 +55,7 @@ Contributors * Waket Zheng ``@waketzheng`` * Yuval Ben-Arie ``@yuvalbenarie`` * Stephan Klein ``@privatwolke`` +* ``@WizzyGeek`` Special Thanks ============== diff --git a/tortoise/contrib/pydantic/utils.py b/tortoise/contrib/pydantic/utils.py index 2c35e36a2..b8984b3fe 100644 --- a/tortoise/contrib/pydantic/utils.py +++ b/tortoise/contrib/pydantic/utils.py @@ -1,8 +1,6 @@ import typing from typing import Any, Callable, Dict, Optional, Type -import tortoise - if typing.TYPE_CHECKING: # pragma: nocoverage from tortoise.models import Model @@ -14,5 +12,4 @@ def get_annotations(cls: "Type[Model]", method: Optional[Callable] = None) -> Di :param method: If specified, we try to get the annotations for the callable :return: The list of annotations """ - globalns = tortoise.Tortoise.apps.get(cls._meta.app, None) if cls._meta.app else None - return typing.get_type_hints(method or cls, globalns=globalns) + return typing.get_type_hints(method or cls)