diff --git a/mypy/semanal.py b/mypy/semanal.py index 11f0156372bf..560d4eeb74e9 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -4014,12 +4014,13 @@ def analyze_alias( python_3_12_type_alias=python_3_12_type_alias, ) - # There can be only one variadic variable at most, the error is reported elsewhere. + # Check for multiple TypeVarTuples in type alias definition new_tvar_defs = [] variadic = False for td in tvar_defs: if isinstance(td, TypeVarTupleType): if variadic: + self.fail("Can only use one TypeVarTuple in a type alias", rvalue) continue variadic = True new_tvar_defs.append(td) diff --git a/test-data/unit/check-python312.test b/test-data/unit/check-python312.test index 0521722b47c1..b09693091d1f 100644 --- a/test-data/unit/check-python312.test +++ b/test-data/unit/check-python312.test @@ -2237,3 +2237,26 @@ 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] + + +[case testMultipleTypeVarTuplesOldStyle] +# flags: --python-version 3.12 +from typing import Union +from typing_extensions import TypeVarTuple, Unpack + +Ts1 = TypeVarTuple("Ts1") +Ts2 = TypeVarTuple("Ts2") + +TA = Union[tuple[*Ts1], tuple[*Ts2]] # E: Can only use one TypeVarTuple in a type alias +[builtins fixtures/tuple.pyi] +[typing fixtures/typing-full.pyi] diff --git a/test-data/unit/check-typevar-tuple.test b/test-data/unit/check-typevar-tuple.test index f6d9de8b0c1a..f9192560cf52 100644 --- a/test-data/unit/check-typevar-tuple.test +++ b/test-data/unit/check-typevar-tuple.test @@ -725,15 +725,18 @@ Ts = TypeVarTuple("Ts") Us = TypeVarTuple("Us") class G(Generic[Unpack[Ts]]): ... -A = Tuple[Unpack[Ts], Unpack[Us]] # E: More than one variadic Unpack in a type is not allowed +A = Tuple[Unpack[Ts], Unpack[Us]] # E: Can only use one TypeVarTuple in a type alias \ + # E: More than one variadic Unpack in a type is not allowed x: A[int, str] reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.str]" -B = Callable[[Unpack[Ts], Unpack[Us]], int] # E: More than one variadic Unpack in a type is not allowed +B = Callable[[Unpack[Ts], Unpack[Us]], int] # E: Can only use one TypeVarTuple in a type alias \ + # E: More than one variadic Unpack in a type is not allowed y: B[int, str] reveal_type(y) # N: Revealed type is "def (builtins.int, builtins.str) -> builtins.int" -C = G[Unpack[Ts], Unpack[Us]] # E: More than one variadic Unpack in a type is not allowed +C = G[Unpack[Ts], Unpack[Us]] # E: Can only use one TypeVarTuple in a type alias \ + # E: More than one variadic Unpack in a type is not allowed z: C[int, str] reveal_type(z) # N: Revealed type is "__main__.G[builtins.int, builtins.str]" [builtins fixtures/tuple.pyi]