Skip to content

Type inference of Tuples returns object instead #4975

Closed
@eddieschoute

Description

@eddieschoute

While interacting with zip I encountered a variant of the following error:

error: No overload variant of "zip" matches argument types [builtins.list[builtins.int*], builtins.object*]

which is emitted by mypy on the case of

out = list(
    list(zip([1, 2], ordered_item))
    for item in [(1, 2), (4, 5)]
    for ordered_item in [item, tuple(reversed(item))]
    )
print(out)

This code runs normally on python 3.6.5, outputting:

[[(1, 1), (2, 2)], [(1, 2), (2, 1)], [(1, 4), (2, 5)], [(1, 5), (2, 4)]]

Expected Behavior

Mypy should infer that ordered_item is a tuple, or at least an iterable so that zip can be applied on it.

It may also be interesting to consider the case where ordered_item is heterogenous over different Iterables (i.e. not only tuples). Would Iterable be inferred then?

Actual Behavior

Mypy infers the object type on ordered_item which you understandably cannot zip.

By explicitly casting item to tuple the error message is resolved:

out = list(
    list(zip([1, 2], ordered_item))
    for item in [(1, 2), (4, 5)]
    for ordered_item in [tuple(item), tuple(reversed(item))]
    )
print(out)

Environment

OS: macOS 10.13
Python: 3.6.5
mypy: 0.590

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions