-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
mypy complains if itertools.chain.from_iterable is given an iterator, tuple, or list. #521
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
Comments
Hm... Maybe Iterable should be covariant? On Saturday, September 3, 2016, Forest Gregg [email protected]
--Guido (mobile) |
Also failing: > chunks = tuple([1] * 3 for _ in range(10))
> flattened = itertools.chain.from_iterable(chunks)
error: Argument 1 to "from_iterable" of "chain" has incompatible type Tuple[Iterable[...], ...]; expected Iterable[Iterable[...]] > chunks = list([1] * 3 for _ in range(10))
> flattened = itertools.chain.from_iterable(chunks)
error: Argument 1 to "from_iterable" of "chain" has incompatible type List[Iterable[...]]; expected Iterable[Iterable[...]] |
I can't actually repro this -- which version of mypy are you using? Can you
show a complete program that reproduces the problem?
|
Here's code that reproduces the problem I ran into. import itertools
from typing import Iterable, Sequence, Tuple
def flat_pairs(blocks : Iterable[Sequence[int]]) -> Iterable[Tuple[int, int]]:
pairs = (itertools.combinations(block, 2) for block in blocks)
return itertools.chain.from_iterable(pairs)
print(list(flat_pairs([[1, 2, 3], [4, 5, 6]])))
mypy 0.4.4 |
I can repro, and I've reduced it to: from typing import *
def flat_pairs(pairs: Iterator[Sequence[int]]) -> Iterable[Tuple[int, int]]:
return pairs The type of pairs here is what is returned by the genexpr in your original repro. The error message from mypy is about the return, and the reason is that That doesn't seem to be a bug in mypy or typeshed, but in your code (or in your expectations of how mypy would work). Note that the error is the same if I change Iterator to Iterable, so that's not the reason either. Also note that putting reveal_type() calls in various places will help show the exact type (in case the error message suppresses some of the type). |
In my It does seem strange that I can't express that fact with It would be very good to have documentation of what types of things are inexpressible. |
It seems like the breakdown is in def combinations(iterable: Iterable[_T], r: int) -> Iterable[Sequence[_T]]: ... My expectations would be met if it was possible to do something like def combinations(iterable: Iterable[_T], r: int) -> Iterable[Tuple[_T * r]]: ... Where the n of the n-tuples in the return type depends upon r. I can imagine why that would be quite difficult. |
I think that would require a concept named "dependent types" where the |
In documentation, |
Opened python/mypy#2115 |
Is valid python code, but mypy throws this error:
The text was updated successfully, but these errors were encountered: