From d9a36c8ae321b59d20d6622b8efa2825f2621be2 Mon Sep 17 00:00:00 2001 From: shiftinv Date: Thu, 26 Oct 2023 21:18:51 +0200 Subject: [PATCH] test: add regression test for dropping cache on namespace change --- tests/test_utils.py | 20 ++++++++++++++++++++ tests/utils_helper_module.py | 3 +++ 2 files changed, 23 insertions(+) diff --git a/tests/test_utils.py b/tests/test_utils.py index a96f18ed54..7a590533b8 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -828,6 +828,26 @@ def test_forwardref_mixed(self) -> None: annotation = utils_helper_module.GenericListAlias["LocalIntOrStr"] assert utils.resolve_annotation(annotation, globals(), locals(), {}) == List[LocalIntOrStr] + # two different forwardrefs with same name + def test_forwardref_duplicate(self) -> None: + DuplicateAlias = int + + # first, resolve an annotation where `DuplicateAlias` resolves to the local int + cache = {} + assert ( + utils.resolve_annotation(List["DuplicateAlias"], globals(), locals(), cache) + == List[int] + ) + + # then, resolve an annotation where the globalns changes and `DuplicateAlias` resolves to something else + # (i.e. this should not resolve to `List[int]` despite {"DuplicateAlias": int} in the cache) + assert ( + utils.resolve_annotation( + utils_helper_module.ListWithDuplicateAlias, globals(), locals(), cache + ) + == List[str] + ) + @pytest.mark.parametrize( ("dt", "style", "expected"), diff --git a/tests/utils_helper_module.py b/tests/utils_helper_module.py index 074e844b32..7711e861b8 100644 --- a/tests/utils_helper_module.py +++ b/tests/utils_helper_module.py @@ -21,3 +21,6 @@ T = TypeVar("T") GenericListAlias = TypeAliasType("GenericListAlias", List[T], type_params=(T,)) + + DuplicateAlias = str + ListWithDuplicateAlias = TypeAliasType("ListWithDuplicateAlias", List["DuplicateAlias"])