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 … Read more

mypy, type hint: Union[float, int] -> is there a Number type?

Use float only, as int is implied in that type: def my_func(number: float): PEP 484 Type Hints specifically states that: Rather than requiring that users write import numbers and then use numbers.Float etc., this PEP proposes a straightforward shortcut that is almost as effective: when an argument is annotated as having type float, an argument … Read more

Using List/Tuple/etc. from typing vs directly referring type as list/tuple/etc

Until Python 3.9 added support for type hinting using standard collections, you had to use typing.Tuple and typing.List if you wanted to document what type the contents of the containers needed to be: def f(points: Tuple[float, float]): return map(do_stuff, points) Up until Python 3.8, tuple and list did not support being used as generic types. … Read more