Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Looses type of nested/inner collection in a flatten(filter(...)) chain #9176

Open
@kaste

Description

@kaste

In the following, very reduced code line d does not typecheck.

from itertools import chain
from typing import List


ints_of_ints: List[List[int]]
c = filter(lambda ints: len(ints), ints_of_ints)  # ok
reveal_type(c)  # typing.Iterator[builtins.list[builtins.int]]

d = chain.from_iterable(filter(lambda ints: len(ints), ints_of_ints))  # mypy: error arg-type - Argument 1 to "len" has incompatible type
                                                                       # "Iterable[int]"; expected "Sized"
reveal_type(d)  # typing.Iterator[builtins.int*]

The interesting or surprising part is that inside the d line mypy thinks that ints is Iterable[int].

Tried mypy 0.782 and mypy-0.790+dev.4cf246f3bb2589d343fe1fdb67a42a23a23c041b.dirty

image

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions