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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
kaste opened this issue Jul 20, 2020 · 1 comment
Open

Comments

@kaste
Copy link

kaste commented Jul 20, 2020

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

@ilevkivskyi
Copy link
Member

I think the issue here is that mypy uses external type context (coming from argument annotation of from_iterable) to infer the filter type variable, instead of second argument type context. This is an infinite story about "single bucket inference". The plan I proposed a while ago for switching to hybrid/compromise inference scheme would solve this (as well as many other similar issues here on the tracker), but it is unlikely to happen anytime soon (or ever).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants