Skip to main content

How to Add a Type Hint for a Tuple in Python

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

A tuple often holds a fixed set of values, such as a screen's width and height. A type hint can say exactly that: tuple[int, int] means "a tuple of two integers". This guide writes tuple hints for a fixed number of items and for any number, on Python 3.15.2.

Syntax​

Syntax
def <function>(<parameter>: tuple[<type 1>, <type 2>]) -> <return type>:

def <function>(<parameter>: tuple[<type>, ...]) -> <return type>:
PartWhat it means
tuple[...]A tuple, written with the built-in tuple and square brackets
<type 1>, <type 2>One type for each item, in order. tuple[int, int] is exactly two integers, and tuple[str, int] is a text followed by a number
<type>, ...Any number of items, all of the same type. The ... is typed as three dots
-> <return type>What the function returns, which can be a tuple[...] too

Tuples are the one container whose hint can give each item its own type. The typing documentation explains it: "tuples are special-cased in Python's typing system. tuple accepts any number of type arguments". And for the second form: "To denote a tuple which could be of any length, and in which all elements are of the same type T, use the literal ellipsis ...: tuple[T, ...]."

The Example​

Two small functions. area takes a screen size as a (width, height) pair of integers and multiplies them. average takes any number of decimal numbers and averages them. The script then prints the hints Python stored for both.

Prerequisites​

  • uv and Python 3.9 or newer. tuple[...] works in hints from Python 3.9. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

area.py
def area(size: tuple[int, int]) -> int:
width, height = size
return width * height


def average(numbers: tuple[float, ...]) -> float:
return sum(numbers) / len(numbers)


print(area((1920, 1080)))
print(average((1.5, 2.5, 3.5)))
print(area.__annotations__)
print(average.__annotations__)

The area.py script in a macOS-style code window: area takes size as a tuple[int, int] and returns an int, average takes numbers as a tuple[float, ...] and returns a float, then four prints: area of (1920, 1080), average of (1.5, 2.5, 3.5), and the __annotations__ of both functions.

size: tuple[int, int] reads "size is a tuple of two integers", and width, height = size takes the two apart. numbers: tuple[float, ...] reads "numbers is a tuple of decimal numbers, as many as you like". __annotations__ is the dictionary where Python keeps a function's hints.

Step 2: Run It​

Terminal
uv run python area.py
Output
2073600
2.5
{'size': tuple[int, int], 'return': <class 'int'>}
{'numbers': tuple[float, ...], 'return': <class 'float'>}

The terminal after uv run python area.py: 2073600, then 2.5, then the hints of area, size tuple[int, int] and return int, and of average, numbers tuple[float, ...] and return float.

A 1920 × 1080 screen has 2073600 pixels, and the three numbers average 2.5. The last two lines show the hints exactly as written: Python kept them, but didn't use them to check anything. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Round brackets: tuple(int, int)The function still ran and printed 2073600. The error came only when the hints were read: TypeError: tuple expected at most 1 argument, got 2Use square brackets: tuple[int, int]
tuple[float] for a tuple of any lengthThat hint means exactly one item: the typing documentation calls tuple[int] "a tuple of length 1"Add , ...: tuple[float, ...]

The first mistake hides because Python 3.14 changed when hints are worked out. The What's New in Python 3.14 page says annotations "are no longer evaluated eagerly. Instead, annotations are stored in special-purpose annotate functions and evaluated only when necessary". So the wrong hint broke only print(area.__annotations__), and the traceback points at __annotate__, the function that works out the hints.

FAQ​

Can the items of a tuple hint have different types?​

Yes. List one type per item, in order: tuple[str, int] is a text followed by a number, such as ("apples", 4).

Does Python stop a tuple of the wrong size?​

No. The typing documentation says: "The Python runtime does not enforce function and variable type annotations. They can be used by third party tools such as type checkers, IDEs, linters, etc." A type checker reads the hints and reports a wrong tuple before the program runs.

Conclusion​

tuple[int, int] described a pair of integers and tuple[float, ...] a tuple of any length. Python stored both hints without checking them, and on Python 3.14 and newer a hint written with round brackets fails only when something reads it.

Additional Resources​