Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4020,6 +4020,8 @@ def analyze_alias(
for td in tvar_defs:
if isinstance(td, TypeVarTupleType):
if variadic:
if not python_3_12_type_alias:
self.fail("Can only use one TypeVarTuple in a type alias", rvalue)
continue
variadic = True
new_tvar_defs.append(td)
Expand Down Expand Up @@ -5641,6 +5643,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 (
Expand Down
23 changes: 23 additions & 0 deletions test-data/unit/check-python312.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Loading