diff --git a/mypy/type_visitor.py b/mypy/type_visitor.py index 86ef6ade8471..35846e7c3ddd 100644 --- a/mypy/type_visitor.py +++ b/mypy/type_visitor.py @@ -51,7 +51,7 @@ get_proper_type, ) -T = TypeVar("T") +T = TypeVar("T", covariant=True) @trait diff --git a/mypy/types.py b/mypy/types.py index e0e897e04cad..38c17e240ccf 100644 --- a/mypy/types.py +++ b/mypy/types.py @@ -3938,8 +3938,12 @@ def visit_type_alias_type(self, t: TypeAliasType, /) -> list[mypy.nodes.TypeAlia assert t.alias is not None if t.alias not in self.seen_alias_nodes: self.seen_alias_nodes.add(t.alias) - return [t.alias] + t.alias.target.accept(self) - return [] + res = [t.alias] + t.alias.target.accept(self) + else: + res = [] + for arg in t.args: + res.extend(arg.accept(self)) + return res def is_named_instance(t: Type, fullnames: str | tuple[str, ...]) -> TypeGuard[Instance]: diff --git a/test-data/unit/check-recursive-types.test b/test-data/unit/check-recursive-types.test index 86e9f02b5263..4f451aa062d6 100644 --- a/test-data/unit/check-recursive-types.test +++ b/test-data/unit/check-recursive-types.test @@ -1014,3 +1014,12 @@ from bogus import Foo # type: ignore A = Callable[[Foo, "B"], Foo] # E: Type alias target becomes "Callable[[Any, B], Any]" due to an unfollowed import B = Callable[[Foo, A], Foo] # E: Type alias target becomes "Callable[[Any, A], Any]" due to an unfollowed import + +[case testRecursiveAliasOnArgumentDetected] +from typing import TypeVar + +T = TypeVar("T") +L = list[T] + +A = L[A] +a: A = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "A")