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"])