Type hints when unpacking a tuple?

PythonPython 3.xType HintingPython TypingIterable Unpacking

Python Problem Overview


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

Python Solutions


Solution 1 - Python

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

a: int
b: int
a, b = t

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCaiView Question on Stackoverflow
Solution 1 - PythonPatrick HaughView Answer on Stackoverflow