Search code examples
pythonpython-typing

Python typing for a tuple


I am simply trying to define typing for a tuple in python 3.85. However, neither approaches in the documentation seem to properly work:

Tuple(float,str)

result:

Traceback (most recent call last):

  File "<ipython-input-30-7964c1934b1f>", line 1, in <module>
    Tuple(float,str)

  File "C:\Users\kinsm\anaconda3\lib\typing.py", line 727, in __call__
    raise TypeError(f"Type {self._name} cannot be instantiated; "

TypeError: Type Tuple cannot be instantiated; use tuple() instead

VERSUS:

tuple(float,str)

result:

Traceback (most recent call last):
  File "<ipython-input-29-fea16b9491a0>", line 1, in <module>
    tuple(float,str)
result:
TypeError: tuple expected at most 1 argument, got 2

Solution

  • The correct syntax is

    from typing import Tuple
    
    ### ---- Examples ----- ###
    Tuple[float, float]
    Tuple[float, str]