Skip to content
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

List gets silently converted into Iterable in itertools.chain.from_iterable #7519

Closed
ruuda opened this issue Sep 16, 2019 · 1 comment
Closed

Comments

@ruuda
Copy link

ruuda commented Sep 16, 2019

  • I am reporting a bug.
  • Consider the following minimal example:
from typing import Callable, Iterable, List, TypeVar
import itertools

T = TypeVar('T')

def tabulate(f: Callable[[int], T]) -> Iterable[T]:
    return map(f, itertools.count(0))

def take_while(p: Callable[[T], bool], xs: Iterable[T]) -> Iterable[T]:
    for x in xs:
        if p(x):
            yield x
        else:
            break

def singleton(i: int) -> List[int]:
    return [i]

fine_1 = take_while(lambda x: len(x) > 0, tabulate(singleton))
fine_2 = itertools.chain.from_iterable(fine_1)

bad = itertools.chain.from_iterable(take_while(lambda x: len(x) > 0, tabulate(singleton)))
  • What is the actual behavior/output?
22: error: Argument 1 to "len" has incompatible type "Iterable[int]"; expected "Sized"
  • What is the behavior/output you expect? I would expect argument 1 to len to take List[int], because that is what T should be instantiated to. I would expect fine_2 and bad to both typecheck; inlining the expression should not change its type.

  • What are the versions of mypy and Python you are using? Mypy 0.720, Python 3.7.4.

  • Do you see the same issue after installing mypy from Git master? Yes, with mypy-0.730+dev.8782ae7f789ad5b8cab97d3c8419f9f98b6eb285.

  • What are the mypy flags you are using? Just the defaults.

Some things I tried:

  • Replace the type var for take_while with a fresh type var U = TypeVar('U'). This did not help.
  • Change the lambdas into defs with type annotations. This did not help.
@ilevkivskyi
Copy link
Member

This is a duplicate of #5738. As you can see from the discussion there, the fix might take some time.

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

No branches or pull requests

2 participants