PyDocs

Nav

Development Tools

TypeVarTuple

typing

class · Added in 3.11
  • TypeVarTuple(name, *, bound=None, covariant=False, contravariant=False, infer_variance=False, default=typing.NoDefault)
Type variable tuple. A specialized form of type variable that enables
variadic generics.

The preferred way to construct a type variable tuple is via the
dedicated syntax for generic functions, classes, and type aliases,
where a single '*' indicates a type variable tuple::

    def move_first_element_to_last[T, *Ts](tup: tuple[T, *Ts]) -> tuple[*Ts, T]:
        return (*tup[1:], tup[0])

Type variables tuples can have default values:

    type AliasWithDefault[*Ts = (str, int)] = tuple[*Ts]

For compatibility with Python 3.11 and earlier, TypeVarTuple objects
can also be created as follows::

    Ts = TypeVarTuple('Ts')  # Can be given any name
    DefaultTs = TypeVarTuple('Ts', default=(str, int))

Just as a TypeVar (type variable) is a placeholder for a single type,
a TypeVarTuple is a placeholder for an *arbitrary* number of types. For
example, if we define a generic class using a TypeVarTuple::

    class C[*Ts]: ...

Then we can parameterize that class with an arbitrary number of type
arguments::

    C[int]       # Fine
    C[int, str]  # Also fine
    C[()]        # Even this is fine

For more details, see PEP 646.

Note that only TypeVarTuples defined in the global scope can be
pickled.
Changed in 3.12, 3.13, 3.15
  • 3.12: Type variable tuples can now be declared using the type parameter syntax introduced by PEP 695.
  • 3.13: Support for default values was added.
  • 3.15: Added support for the bound, covariant, contravariant, and infer_variance parameters.
Members of TypeVarTuple

_

__contravariant__
Whether the type variable tuple has been explicitly marked as contravariant.
__covariant__
Whether the type variable tuple has been explicitly marked as covariant.
__default__
The default value for this TypeVarTuple.
__infer_variance__
Whether the type variable tuple’s variance should be inferred by type checkers.
__name__
The name of the type variable tuple.

e

evaluate_default
An evaluate function corresponding to the __default__ attribute.

h

has_default
Return whether or not the type variable tuple has a default value.
0 Examples
No examples for typing/TypeVarTuple.
See Also
No see-alsos for typing/TypeVarTuple
0 Notes
No notes for TypeVarTuple