Skip to content

Failure to correctly infer tuple type through zip #8454

Closed
python/typeshed
#3830
@JonathanShor

Description

@JonathanShor

The following code runs correctly on Python 3.7.3:

vals = (("zz", 1), ("yyy", 8))
EXAMPLE: Dict[str, List[Union[str, int]]] = { k: list(val) for k, val in zip(["A", "B"], zip(*vals)) }

producing
>>> EXAMPLE {'A': ['zz', 'yyy'], 'B': [1, 8]}

but raises the following error with mypy 0.761 (and on https://mypy-play.net/):

error: Argument 1 to "list" has incompatible type "Tuple[object, object]"; expected "Iterable[Union[str, int]]"

Explicitly typing the tuple as follows removes the error:
vals: Tuple[Tuple[str, int], ...] = (("zz", 1), ("yyy", 8))

Interesting, explicitly typing the tuple and the nested tuples does not remove the error:
vals: Tuple[Tuple[str, int], Tuple[str, int]] = (("zz", 1), ("yyy", 8))
produces the same error above when no type hint is provided.

Possibly related to #4975 ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions