Skip to content

Commit 16cd4c5

Browse files
authored
Traverse type alias args in collect visitor (#19943)
Fixes #19941 Fix is trivial (and a bit embarrassing, LOL).
1 parent 89f7223 commit 16cd4c5

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

mypy/type_visitor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
get_proper_type,
5252
)
5353

54-
T = TypeVar("T")
54+
T = TypeVar("T", covariant=True)
5555

5656

5757
@trait

mypy/types.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3938,8 +3938,12 @@ def visit_type_alias_type(self, t: TypeAliasType, /) -> list[mypy.nodes.TypeAlia
39383938
assert t.alias is not None
39393939
if t.alias not in self.seen_alias_nodes:
39403940
self.seen_alias_nodes.add(t.alias)
3941-
return [t.alias] + t.alias.target.accept(self)
3942-
return []
3941+
res = [t.alias] + t.alias.target.accept(self)
3942+
else:
3943+
res = []
3944+
for arg in t.args:
3945+
res.extend(arg.accept(self))
3946+
return res
39433947

39443948

39453949
def is_named_instance(t: Type, fullnames: str | tuple[str, ...]) -> TypeGuard[Instance]:

test-data/unit/check-recursive-types.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,3 +1014,12 @@ from bogus import Foo # type: ignore
10141014

10151015
A = Callable[[Foo, "B"], Foo] # E: Type alias target becomes "Callable[[Any, B], Any]" due to an unfollowed import
10161016
B = Callable[[Foo, A], Foo] # E: Type alias target becomes "Callable[[Any, A], Any]" due to an unfollowed import
1017+
1018+
[case testRecursiveAliasOnArgumentDetected]
1019+
from typing import TypeVar
1020+
1021+
T = TypeVar("T")
1022+
L = list[T]
1023+
1024+
A = L[A]
1025+
a: A = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "A")

0 commit comments

Comments
 (0)