TypeVarTuple allows variadic generics (variable number of type parameters).
1from typing import TypeVarTuple23Ts = TypeVarTuple("Ts")45# Variadic generic class6class Array(*tuple[Ts]):7 def __init__(self, *args: *Ts):8 self.data = args910# Type-safe usage11a: Array[int, str, float] = Array(1, "hello", 3.14)1213# With TypeVar14from typing import TypeVar1516T = TypeVar("T")1718class Tuple(*tuple[T, *Ts]):19 pass2021# In practice — used for typed *args22from typing import TypeVarTuple, Unpack2324Ts = TypeVarTuple("Ts")2526def merge(*args: *Ts) -> tuple[*Ts]:27 return args2829result = merge(1, "hello", 3.14) # tuple[int, str, float]
Benefits: