pythonpython-3.xtype-hintingpython-typingiterable-unpacking

Type hints when unpacking a tuple?


Is it possible to use type hinting when unpacking a tuple? I want to do this, but it results in a SyntaxError:

from typing import Tuple

t: Tuple[int, int] = (1, 2)
a: int, b: int = t
#     ^ SyntaxError: invalid syntax

Solution

  • According to PEP-0526, you should annotate the types first, then do the unpacking

    a: int
    b: int
    a, b = t