Skip to content

Commit 24ca4d5

Browse files
committed
Tracks seen aliases by TypeAlias node instead of TypeAliasType instance in semanal_typeargs.py and
server/deps.py, consistent with the fix applied to type_visitor.py.
1 parent b13cc7c commit 24ca4d5

2 files changed

Lines changed: 15 additions & 12 deletions

File tree

mypy/semanal_typeargs.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from mypy.message_registry import INVALID_PARAM_SPEC_LOCATION, INVALID_PARAM_SPEC_LOCATION_NOTE
1616
from mypy.messages import format_type
1717
from mypy.mixedtraverser import MixedTraverserVisitor
18-
from mypy.nodes import Block, ClassDef, Context, FakeInfo, FuncItem, MypyFile
18+
from mypy.nodes import Block, ClassDef, Context, FakeInfo, FuncItem, MypyFile, TypeAlias
1919
from mypy.options import Options
2020
from mypy.scope import Scope
2121
from mypy.subtypes import is_same_type, is_subtype
@@ -58,9 +58,11 @@ def __init__(
5858
self.scope = Scope()
5959
# Should we also analyze function definitions, or only module top-levels?
6060
self.recurse_into_functions = True
61-
# Keep track of the type aliases already visited. This is needed to avoid
62-
# infinite recursion on types like A = Union[int, List[A]].
63-
self.seen_aliases: set[TypeAliasType] = set()
61+
# Keep track of the type alias definitions already visited. This is needed
62+
# to avoid infinite recursion on recursive type aliases. We track by the
63+
# underlying TypeAlias node (not TypeAliasType) so that recursive aliases
64+
# with varying type arguments are still caught.
65+
self.seen_aliases: set[TypeAlias] = set()
6466

6567
def visit_mypy_file(self, o: MypyFile) -> None:
6668
self.errors.set_file(o.path, o.fullname, scope=self.scope, options=self.options)
@@ -84,10 +86,10 @@ def visit_block(self, o: Block) -> None:
8486
def visit_type_alias_type(self, t: TypeAliasType) -> None:
8587
super().visit_type_alias_type(t)
8688
if t.is_recursive:
87-
if t in self.seen_aliases:
89+
if t.alias in self.seen_aliases:
8890
# Avoid infinite recursion on recursive type aliases.
8991
return
90-
self.seen_aliases.add(t)
92+
self.seen_aliases.add(t.alias)
9193
assert t.alias is not None, f"Unfixed type alias {t.type_ref}"
9294
is_error, is_invalid = self.validate_args(
9395
t.alias.name, tuple(t.args), t.alias.alias_tvars, t
@@ -105,7 +107,7 @@ def visit_type_alias_type(self, t: TypeAliasType) -> None:
105107
# to verify the arguments satisfy the upper bounds of the expansion as well.
106108
get_proper_type(t).accept(self)
107109
if t.is_recursive:
108-
self.seen_aliases.discard(t)
110+
self.seen_aliases.discard(t.alias)
109111

110112
def visit_tuple_type(self, t: TupleType) -> None:
111113
t.items = flatten_nested_tuples(t.items)

mypy/server/deps.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ class 'mod.Cls'. This can also refer to an attribute inherited from a
122122
StarExpr,
123123
SuperExpr,
124124
TupleExpr,
125+
TypeAlias,
125126
TypeAliasExpr,
126127
TypeApplication,
127128
TypedDictExpr,
@@ -949,18 +950,18 @@ def get_type_triggers(self, typ: Type) -> list[str]:
949950

950951

951952
def get_type_triggers(
952-
typ: Type, use_logical_deps: bool, seen_aliases: set[TypeAliasType] | None = None
953+
typ: Type, use_logical_deps: bool, seen_aliases: set[TypeAlias] | None = None
953954
) -> list[str]:
954955
"""Return all triggers that correspond to a type becoming stale."""
955956
return typ.accept(TypeTriggersVisitor(use_logical_deps, seen_aliases))
956957

957958

958959
class TypeTriggersVisitor(TypeVisitor[list[str]]):
959960
def __init__(
960-
self, use_logical_deps: bool, seen_aliases: set[TypeAliasType] | None = None
961+
self, use_logical_deps: bool, seen_aliases: set[TypeAlias] | None = None
961962
) -> None:
962963
self.deps: list[str] = []
963-
self.seen_aliases: set[TypeAliasType] = seen_aliases or set()
964+
self.seen_aliases: set[TypeAlias] = seen_aliases or set()
964965
self.use_logical_deps = use_logical_deps
965966

966967
def get_type_triggers(self, typ: Type) -> list[str]:
@@ -979,9 +980,9 @@ def visit_instance(self, typ: Instance) -> list[str]:
979980
return triggers
980981

981982
def visit_type_alias_type(self, typ: TypeAliasType) -> list[str]:
982-
if typ in self.seen_aliases:
983+
if typ.alias in self.seen_aliases:
983984
return []
984-
self.seen_aliases.add(typ)
985+
self.seen_aliases.add(typ.alias)
985986
assert typ.alias is not None
986987
trigger = make_trigger(typ.alias.fullname)
987988
triggers = [trigger]

0 commit comments

Comments
 (0)