Yes, you can make the number of items in a tuple type hint flexible.

From Python 3.9 and on you can do:

def func(var: tuple[int, ...]):
    pass

Before Python 3.9 you would do:

from typing import Tuple

def func(var: Tuple[int, ...]):
    pass

From the docs: https://docs.python.org/3/library/stdtypes.html#tuple

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.

Answer from aaron on Stack Overflow
🌐
Teclado
teclado.com › 30-days-of-python › python-30-day-28-type-hinting
Day 28: Type Hinting | Teclado
We're not currently telling Python what our functions should return. We can do this by using -> after the parentheses when defining our functions. Most of our functions don't need a return annotation, because they implicitly return. However, our find_movie function does, and it can return two different values: a movie tuple, or None. It returns None in the case where no matching movie was found. ... def find_movie(search_term: str, movies: list[Movie]) -> Union[Movie, None]: for title, director, year in movies: if title == search_term: return (title, director, year)
🌐
flycoolman
flycoolman.com › coding › python › list-tuple-dict-vs-list-tuple-dict
list, tuple, dict vs List, Tuple, Dict | flycoolman
October 4, 2020 - This specifies that the tuple passed in must contain two float values. You can’t do this with the built-in tuple type before Python 3.9. Python 3.9 has the change that built-in types support hints
Discussions

Tuple-like typing for lists in type hints
Hello python devs, I always run into an issue like this while working with Union data, so like I know what elements will be in a list, but I don't think there's a way to type them, so I alw... More on github.com
🌐 github.com
1
October 1, 2022
Type Hints, can you indicate various types inside a list?
You could use List[Union[str, int]] for a list that contains strings and ints, but that is not what you have here. This is actually a list of tuples, each of which has a str and an int. For that you can use List[Tuple[str, int]] which specifically says that each tuple contains two items, the first of which is a string and the second an int. Also note that on Python 3.9+ you should use the built-in types rather than the ones defined in typing, so list[tuple[str, int]]. And note again that in your example list_of_grades[0] isn't a string, it's a tuple of (string, int). More on reddit.com
🌐 r/learnpython
7
2
November 15, 2021
Correct way to typehint an argument that only accepts list, set, or tuple - Typing - Discussions on Python.org
is Union[set[T], tuple[T, ...], list[T]] correct? If so, what would be the “correct” alias for such a type? Or is there already a type in stdlib that I should use instead More on discuss.python.org
🌐 discuss.python.org
0
July 6, 2024
Question regarding type-hinting Collection types
So, logic says that here: list[str | int] It means that it's a list that contains ONLY strings OR ONLY integers. Right? Wrong, actually. This is a list of mixed str and int. What you mean would be written list[str] | list[int]. More on reddit.com
🌐 r/Python
10
8
August 26, 2023
🌐
GitHub
github.com › python › typing › issues › 1267
Tuple-like typing for lists in type hints · Issue #1267 · python/typing
October 1, 2022 - x: Dict[str, List[int, int]] = { "hi": [1, 2], "bye": [3, 1], "aaa": [79, 27], "haxxor": [1337, 420, 69], # error: Incompatible types in assignment (expression has type "List[int, int, int]", variable has type "List[int, int]") } Unions aren't as convenient... ... topic: featureDiscussions about new features for Python's type annotationsDiscussions about new features for Python's type annotations
Published   Oct 01, 2022
🌐
Reddit
reddit.com › r/learnpython › type hints, can you indicate various types inside a list?
r/learnpython on Reddit: Type Hints, can you indicate various types inside a list?
November 15, 2021 -

If I have a list that I know will have a certain type of object in the first element and a different type in the second. For instance, if I have a function that combines two lists and returns a lists of lists like so:

names = ["Brad Pitt", "Leonardo DiCaprio", "Henry Cavill", "Hugo Weaving"]
grades = [85,32,95,90]

def combine_lists():
    names_grades = []
    for i in range(0,4):
        names_grades.append(names[i],grades[i])
    return names_grades

Is it possible to type hint the return type so that it reflects the types in the list, something like this:

def combine_lists() ->List(str,int):

The point of this is that later I would be aware of the type I'm accessing when I try to read a specific element of the list for example:

list_of_grades = combine_lists()x = list_of_grades[0] #I want to know this is a string

🌐
Python.org
discuss.python.org › typing
Correct way to typehint an argument that only accepts list, set, or tuple - Typing - Discussions on Python.org
July 6, 2024 - is Union[set[T], tuple[T, ...], list[T]] correct? If so, what would be the “correct” alias for such a type? Or is there already a type in stdlib that I should use instead?
🌐
Substack
zlliu.substack.com › p › 4-python-type-hint-things-i-shouldve
4 Python Type Hint Things I Should’ve Known Earlier
July 19, 2025 - To make our type hint more flexible, we can replace list[int] with Sequence[int]. Since lists, tuples, etc are all sequences, our users can pass in any of those without our type checker complaining. ^ notice that my return value type hint is still list[int]. As the function author, I know for sure that my output will be a list. Type hints for return values should thus be specific instead of generic. In older code bases, you might see stuff like this: Note — since Python 3.9, these type hints have been deprecated.
Find elsewhere
🌐
Byby
byby.dev › py-tuple-type-hints
How to use tuple type hints in Python
December 12, 2023 - # Create a tuple my_tuple = ("apple", "banana", "cherry") # Access elements print(my_tuple[0]) # Output: apple print(my_tuple[1]) # Output: banana print(my_tuple[-1]) # Output: cherry # Looping through a tuple for fruit in my_tuple: print(fruit) # Tuple unpacking a, b, c = my_tuple print(a) # Output: apple print(b) # Output: banana print(c) # Output: cherry # Tuples are immutable try: my_tuple[0] = "grapefruit" except TypeError: print("Tuples are immutable") While Python is dynamically typed, meaning you don’t have to explicitly declare the type of a variable, type hints are a form of static typing that can help catch certain types of errors early in development and provide documentation.
🌐
Chris's Wiki
utcc.utoronto.ca › ~cks › space › blog › python › TypeHintsAndMyPyNotes
Chris's Wiki :: blog/python/TypeHintsAndMyPyNotes
November 27, 2024 - When you work with data structures created from built in collections, you can wind up with long, tangled compound type name, like 'tuple[str, list[tuple[str, int]]]' (which is a real type in my program). These are annoying to keep typing and easy to make mistakes with, so Python type hints provide two ways of giving them short names, in type aliases and typing.NewType.
🌐
Mypy
mypy.readthedocs.io › en › stable › cheat_sheet_py3.html
Type hints cheat sheet - mypy 1.19.1 documentation
# For most types, just use the name of the type in the annotation # Note that mypy can usually infer the type of a variable from its value, # so technically these annotations are redundant x: int = 1 x: float = 1.0 x: bool = True x: str = "test" x: bytes = b"test" # For collections on Python 3.9+, the type of the collection item is in brackets x: list[int] = [1] x: set[int] = {6, 7} # For mappings, we need the types of both keys and values x: dict[str, float] = {"field": 2.0} # Python 3.9+ # For tuples of fixed size, we specify the types of all the elements x: tuple[int, str, float] = (3, "yes
🌐
Real Python
realpython.com › python-type-hints-multiple-types
How to Use Type Hints for Multiple Return Types in Python – Real Python
March 8, 2024 - For example, a function may accept ... a list, tuple, or generator, but always return a list. If your function can return several different types, then you should first consider whether you can refactor it to have a single return type. In this tutorial, you’ll learn how to deal with those functions that need multiple return types. To get the most out of this tutorial, you should know the basics of what type hints in Python are and how ...
🌐
Reddit
reddit.com › r/python › question regarding type-hinting collection types
r/Python on Reddit: Question regarding type-hinting Collection types
August 26, 2023 -

I'm trying to make sense of the current type-hinting convention regarding Collection types. For simplicity's sake, I'll use list, tuple and dict, but that applies to any of their variant. Note that none of the following lines of code actually raise an error when run. Here's my question:

When someone writes:

var: str | int

What I read is obviously that 'var' is either a string OR an integer.

Again, when someone writes:

lst: list[str]

What I read is obviously that 'lst' is a list that contains ONLY strings.

So, logic says that here:

list[str | int]

It means that it's a list that contains ONLY strings OR ONLY integers. Right? And in this following case:

list[str, int]

it means that it's a list that contains strings AND/OR integers. Perhaps would it make more sense if it used & instead of , but whatever. Hopefully I'm making sense.

However, when checking the types using mypy, I find out that list[str, int] is not conventional, and that rather you should use list[str | int] for both cases. My code editor also says this, and yours probably does too.

Now take a similarly-structured type, tuple. By similarly-structured, I mean that you access and iterate through tuples the same way, in fact they are both Sequence types. The thing with tuple is that its args are index sensitive. I understand why this is the case and why it makes sense (because of immutability), but what doesn't make sense to me is why mypy is fine now with the comma notation with tuple but not list.

tpl: tuple[str, int] = ("", 0) # fine
tpl: tuple[str, int] = (0, "") # not fine
tpl: tuple[str | int] = (0, "") # not fine
tpl: tuple[str | int] = (0, ) # fine
tpl: tuple[str | int] = ("", ) # fine

Last but not least, dict. Everybody knows that Mapping types are different from Sequence types in the way you access, set or delete their items. So why is the typing convention this:

dict[str, int] # comma notation like tuple

and not:

dict[str: int] # Mapping specific notation 

The latter makes much more sense and is more readable to me, and makes the distinction between Sequence types and Mapping types. Furthermore, the use of commas in Mapping types kinda looks like you can insert as many of them as you want, just like tuple?

tpl: tuple[str, int] = ("", 0) # this is fine
tpl: tuple[str, int, bool] = ("", 0, False) # this is fine
dct: dict[str, int, bool] = {"": 0: False} # but this is not fine

I know that slices are not hashable for a reason, and that dict[str: int] notation kinda implies that they are, but again, the interpreter will raise no errors if you do this, much like if you write list[str, int], so I don't see the problem.

Is there an actual, good reason why this is the way it is?

Top answer
1 of 4
21
So, logic says that here: list[str | int] It means that it's a list that contains ONLY strings OR ONLY integers. Right? Wrong, actually. This is a list of mixed str and int. What you mean would be written list[str] | list[int].
2 of 4
10
You're looking at this purely from the perspective of a "user", and not the perspective of the "author". Let's pretend for a moment that the builtin class list doesn't exist, and you have to implement it. To do that, you need typing.Generic and, more importantly, a typing.TypeVar . The code would look more or less like this: T = typing.TypeVar('T') class List(typing.Generic[T]): def __init__(self): self._elements = [] def append(self, element: T) -> None: self._elements.append(element) As you can see, this class is generic over one TypeVar. So naturally, it also accepts one type argument: >>> List[int] # one argument __main__.List[int] >>> List[int | str] # still one argument __main__.List[int | str] >>> List[int, str] # two arguments TypeError: Too many arguments for ; actual 2, expected 1 (The reason why list[int, str] doesn't throw an error is because the python devs are lazy.) As for tuples, they serve a different purpose than lists. Lists are intended to be a homogeneous data structure, i.e. a sequence of an arbitrary number of items of the same type. Tuples, on the other hand, are intended to have a fixed number of items of (potentially) different types. So naturally tuple must accept an arbitrary number of type arguments. dict[str: int] # Mapping specific notation The latter makes much more sense and is more readable to me, and makes the distinction between Sequence types and Mapping types. This would make python's syntax and parser more complicated - and, frankly, inconsistent - for no real reason. As far as the type system is concerned, there is no difference between a sequence and a mapping. They're both just Generics, albeit with a different number of TypeVars.
🌐
FastAPI Tutorial
fastapitutorial.com › blog › advanced-type-hints
2. Advanced Type Hints
In the newer versions of Python, we can directly use the list, tuple, and dict keywords. new_price: list[int] = [14,902,898] new_immutable_price: tuple[int,int,int] = (388,392,299) new_price_dict: dict[str,int] = { "item_1":240, "item_2":490, } 2. Annotating with complex data structures ⚠️ For now, I am proceeding with the older and established syntax for type annotations. Python does support a newer way to write type hints but there are some issues with it.
🌐
Safjan
safjan.com › home › note › python - how to make type hint for the tuple...
Python - How to Make Type Hint for the Tuple With Undetermined Number of Strings?
July 11, 2023 - Learn how to use Python's typing.Tuple and Union with ellipsis (...) to create type hints for tuples containing an undetermined number of strings.
🌐
Educative
educative.io › answers › what-are-type-hints-in-python
What are Type hints in Python?
from typing import List, Set, Dict, Tuple · Note: In Python 3.9+, in the declarations for type hints for collections, the collection type is not capitalized: list_int: list[int] = [3] A function signature has the following: Name of the function · Parameters of the function ·
🌐
Python documentation
docs.python.org › 3 › library › typing.html
typing — Support for type hints
1 week ago - 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, ...]. To denote an empty tuple, use tuple[()]. Using plain tuple as an annotation is equivalent to using ...
🌐
Python
docs.python.org › 3.9 › library › typing.html
typing — Support for type hints — Python 3.9.24 documentation
The argument list must be a list of types or an ellipsis; the return type must be a single type. There is no syntax to indicate optional or keyword arguments; such function types are rarely used as callback types. Callable[..., ReturnType] (literal ellipsis) can be used to type hint a callable taking any number of arguments and returning ReturnType.
🌐
FastAPI
fastapi.tiangolo.com › python-types
Python Types Intro - FastAPI
Notice that the variable item is one of the elements in the list items. And still, the editor knows it is a str, and provides support for that. ... The variable items_t is a tuple with 3 items, an int, another int, and a str.
🌐
Python.org
discuss.python.org › typing
What are the subtyping rules for tuple[T, ...]? - Page 3 - Typing - Discussions on Python.org
November 30, 2023 - PEP 484 introduced a way to express “arbitrary-length homogeneous tuples”, but it didn’t specify the subtyping rules for these types. Arbitrary-length homogeneous tuples can be expressed using one type and ellipsis, fo…