Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeHinting tuples in Python

When I want to typehint a tuple in Python like:

def func(var: tuple[int, int]):     # do something  func((1, 2))    # would be fine func((1, 2, 3)) # would throw an error 

It is required to give the exact number of items in the tuple. That's different from list typehinting:

def func(var: list[int]):     # do something  func([1])       # would be fine func([1, 2])    # would also be fine func([1, 2, 3]) # would also be fine 

That's consequentially, in a way, because of the type of tuples. Because they are designed not to be changed, you have to hardcode the amount of items in it.

So my question is, is there a way to make the number of items in a tuple type hint flexible? I tried something like that but it didn't work:

def func(var: tuple[*int]): 
like image 301
Asara Avatar asked Nov 28 '17 14:11

Asara


People also ask

What is type hinting in Python?

Introduction to Python type hints It means that you need to declare types of variables, parameters, and return values of a function upfront. The predefined types allow the compilers to check the code before compiling and running the program.

What is typing tuple in Python?

Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage. A tuple is a collection which is ordered and unchangeable. Tuples are written with round brackets.

How do you Typehint a 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 .

How do you use a typing tuple?

typing. Tuple is special here in that it lets you specify a specific number of elements expected and the type of each position. Use ellipsis if the length is not set and the type should be repeated: Tuple[float, ...] describes a variable-length tuple with float s. For typing.

What is a tuple in Python with example?

Tuple type; Tuple[X, Y] is the type of a tuple of two items with the first item of type X and the second of type Y. Example: Tuple[T1, T2] is a tuple of two elements corresponding to type variables T1 and T2.

How to set type hints for multiple types in Python?

To set type hints for multiple types, you can use Union from the typing module. Second, use the Union to create a union type that includes int and float: Starting from Python 3.10, you can use the X | Y syntax to create a union type, for example: Python allows you to assign an alias to a type and use the alias for type hintings. For example:

How do you change the number of items in tuples in Python?

Tuples are unchangeable, meaning that we cannot change, add or remove items after the tuple has been created. To determine how many items a tuple has, use the len () function: To create a tuple with only one item, you have to add a comma after the item, otherwise Python will not recognize it as a tuple.

What is an empty tuple in C++?

Tuple type; Tuple [X, Y] is the type of a tuple of two items with the first item of type X and the second of type Y. The type of the empty tuple can be written as Tuple [ ()]. Example: Tuple [T1, T2] is a tuple of two elements corresponding to type variables T1 and T2. Tuple [int, float, str] is a tuple of an int, a float and a string.


1 Answers

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

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

From the docs: https://docs.python.org/3/library/typing.html#typing.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.

like image 114
aaron Avatar answered Sep 24 '22 08:09

aaron