Skip to content

Unpacking starred expressions in tuples #7779

Closed
@goodmami

Description

@goodmami

(migrated from a comment in #1178 at @ethanhs's request)

There seems to an issue with unpacking things when they are part of a tuple on the right side of assignment. #1178 is related, but specifically deals with variable-length tuples (and using len() to distinguish them). The current issue occurs even for fixed-length and type-annotated tuples.

# file: ex.py
from typing import Tuple
points = (1, 2)  # type: Tuple[int, int]
x, y, z = *points, 0

This results in incorrect inference about the tuple size (but Python does not complain, only mypy):

$ mypy ex.py 
ex.py:4: error: Need more than 2 values to unpack (3 expected)
Found 1 error in 1 file (checked 1 source file)

A workaround is to use cast(), as @JukkaL suggested:

x, y, z = cast(Tuple[int, int, int], (*points, 0))

And here's my versions of Python and mypy:

$ mypy --version
mypy 0.740
$ python --version
Python 3.7.3

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions