From bb7190b5a1e1ee271341a102b2d50aa2b6e920a1 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Mon, 14 Apr 2025 00:32:29 +0200 Subject: [PATCH] Fix PEP 695 type alias with mix of type args (PEP 696) --- mypy/semanal.py | 2 +- test-data/unit/check-python313.test | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index 6d0a62070c8e..586094b7a6fe 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -5591,7 +5591,7 @@ def visit_type_alias_stmt(self, s: TypeAliasStmt) -> None: self.msg.unimported_type_becomes_any("Type alias target", res, s) res = make_any_non_unimported(res) eager = self.is_func_scope() - if isinstance(res, ProperType) and isinstance(res, Instance) and not res.args: + if isinstance(res, ProperType) and isinstance(res, Instance): fix_instance(res, self.fail, self.note, disallow_any=False, options=self.options) alias_node = TypeAlias( res, diff --git a/test-data/unit/check-python313.test b/test-data/unit/check-python313.test index 2729ad3e21d1..f020b1602b99 100644 --- a/test-data/unit/check-python313.test +++ b/test-data/unit/check-python313.test @@ -219,7 +219,7 @@ def func_a1( reveal_type(b) # N: Revealed type is "builtins.dict[builtins.float, builtins.str]" reveal_type(c) # N: Revealed type is "builtins.dict[builtins.float, builtins.float]" reveal_type(d) # N: Revealed type is "builtins.dict[builtins.int, builtins.str]" -[builtins fixtures/tuple.pyi] +[builtins fixtures/dict.pyi] [typing fixtures/typing-full.pyi] [case testPEP695TypeParameterDefaultTypeAlias2] @@ -255,3 +255,22 @@ def func_c1( [builtins fixtures/tuple.pyi] [typing fixtures/typing-full.pyi] + +[case testPEP695TypeParameterDefaultTypeAlias4] +# flags: --disallow-any-generics +class A[L = int, M = str]: ... +TD1 = A[float] +type TD2 = A[float] + +def func_d1( + a: TD1, + b: TD1[float], # E: Bad number of arguments for type alias, expected 0, given 1 + c: TD2, + d: TD2[float], # E: Bad number of arguments for type alias, expected 0, given 1 +) -> None: + reveal_type(a) # N: Revealed type is "__main__.A[builtins.float, builtins.str]" + reveal_type(b) # N: Revealed type is "__main__.A[builtins.float, builtins.str]" + reveal_type(c) # N: Revealed type is "__main__.A[builtins.float, builtins.str]" + reveal_type(d) # N: Revealed type is "__main__.A[builtins.float, builtins.str]" +[builtins fixtures/tuple.pyi] +[typing fixtures/typing-full.pyi]