From 0af3fd56dae8a1002071acc37e71e9c79361342d Mon Sep 17 00:00:00 2001 From: aviralgarg05 Date: Fri, 9 Jan 2026 23:42:40 +0530 Subject: [PATCH] Report error when type alias has multiple TypeVarTuples (PEP 695) When defining a type alias using the PEP 695 syntax with multiple TypeVarTuple-based type parameters (e.g., `type TA[*Ts1, *Ts2] = ...`), mypy now reports an error at definition time rather than only when the alias is used. This is consistent with how mypy handles classes with multiple TypeVarTuples and matches the behavior of other type checkers like pyright and pyrefly. Fixes #20544. --- mypy/semanal.py | 9 +++++++++ test-data/unit/check-python312.test | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/mypy/semanal.py b/mypy/semanal.py index 11f0156372bf..5a929db74707 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -5641,6 +5641,15 @@ def visit_type_alias_stmt(self, s: TypeAliasStmt) -> None: return all_type_params_names = [p.name for p in s.type_args] + # Check for multiple TypeVarTuples in type alias definition + has_type_var_tuple = False + for p in s.type_args: + if p.kind == TYPE_VAR_TUPLE_KIND: + if has_type_var_tuple: + self.fail("Can only use one TypeVarTuple in a type alias", s) + break + has_type_var_tuple = True + try: existing = self.current_symbol_table().get(s.name.name) if existing and not ( diff --git a/test-data/unit/check-python312.test b/test-data/unit/check-python312.test index 0521722b47c1..f7a567fbd7ba 100644 --- a/test-data/unit/check-python312.test +++ b/test-data/unit/check-python312.test @@ -2237,3 +2237,13 @@ class D[*Ts](Generic[Unpack[Us]]): # E: Generic[...] base class is redundant \ # E: Can only use one type var tuple in a class def pass [builtins fixtures/tuple.pyi] + +[case testPEP695MultipleTypeVarTuplesTypeAlias] +type TA1[*Ts1, *Ts2] = tuple[*Ts1] | tuple[*Ts2] # E: Can only use one TypeVarTuple in a type alias +type TA2[T, *Ts1, *Ts2] = tuple[T, *Ts1, *Ts2] # E: Can only use one TypeVarTuple in a type alias \ + # E: More than one variadic Unpack in a type is not allowed +type TA3[*Ts1, T, *Ts2] = tuple[*Ts1, T, *Ts2] # E: Can only use one TypeVarTuple in a type alias \ + # E: More than one variadic Unpack in a type is not allowed +type TA4[*Ts] = tuple[*Ts] # OK - single TypeVarTuple is fine +[builtins fixtures/tuple.pyi] +[typing fixtures/typing-full.pyi]