How to annotate function that takes a tuple of variable length? (variadic tuple type annotation)

We can annotate variable-length homogeneous tuples using the ... literal (aka Ellipsis) like this:

def process_tuple(t: Tuple[str, ...]):
    ...

or for Python3.9+

def process_tuple(t: tuple[str, ...]):
    ...

After that, the errors should go away.

From the docs:

To specify a variable-length tuple of homogeneous type, use literal
ellipsis, e.g. Tuple[int, ...]. A plain Tuple is equivalent to
Tuple[Any, ...], and in turn to tuple.

Leave a Comment