Python glossary entry

TypeVarTuple

typing.TypeVarTuple
Namespace
Standard library
Module
typing
Available since
Python 3.11

Represents a variable-length tuple of type variables for APIs whose type shape has an arbitrary number of dimensions or positions.

Definition

A TypeVarTuple captures zero or more type arguments. Where a normal type variable stands for one type, a type-variable tuple stands for an arbitrary type sequence.

A variadic generic shape

Pyodide / WebAssembly
from typing import Generic, TypeVarTuple

Shape = TypeVarTuple("Shape")

class Array(Generic[*Shape]):
    pass

Vector = Array[int]
Matrix = Array[int, int]

print("[state] declared variadic parameter:", Shape)
print("[result] one-dimensional alias:", Vector)
print("[result] two-dimensional alias:", Matrix)

Static tools can use each unpacked position to preserve a shape. Runtime aliases expose typing metadata, but Python does not enforce dimensions when an instance is constructed.

Use it when

Variadic generics are appropriate for array shapes, heterogeneous argument tuples, and APIs where the number of type positions is itself generic. A fixed tuple or ordinary TypeVar is clearer when the number of positions is known.

Common mistake

A type-variable tuple must be unpacked. Modern syntax uses *Shape; typing.Unpack[Shape] provides the equivalent spelling where starred type syntax is unavailable.

Primary sourceOfficial Python documentation ↗