Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python typing: tuple[str] vs tuple[str, ...]

What's the difference between tuple[str, ...] vs tuple[str]? (python typing)

like image 764
ikamen Avatar asked Dec 17 '25 13:12

ikamen


1 Answers

giving a tuple type with ellipsis means that number of elements in this tuple is not known, but type is known:

x: tuple[str, ...] = ("hi",)          #VALID
x: tuple[str, ...] = ("hi", "world")  #ALSO VALID

not using ellipsis means a tuple with specific number of elements, e.g.:

y: tuple[str] = ("hi", "world")  # Type Warning: Expected type 'Tuple[str]', got 'Tuple[str, str]' instead 

This goes in contrast to notation of other collections, e.g. list[str] means list of any length with elements of type str.

like image 173
ikamen Avatar answered Dec 19 '25 03:12

ikamen