Skip to content

Combination of collections where element types are compatible only works left-to-right #5948

Closed
@PeterJCLaw

Description

@PeterJCLaw

Currently mypy supports combining a list of integers to a list of floats:

y = [float('inf')] + list(range(10))

and the resulting type is builtins.list[builtins.float*].

It would be great if this worked with the collections in the reverse order:

x = list(range(10)) + [float('inf')]

Currently the latter errors with List item 0 has incompatible type "float"; expected "int".

I'm not sure whether this is a limitation of general support for combining generic collections, or there's something special for extending lists of floats by lists of integers.

Either way, it might also be good if this was a general thing for all collections of compatible generics.

Currently I'm using a cast so that my code checks ok, though I'd like to be able to remove that.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions