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

Mypy doesn't recognize objects implementing __iter__ as being Iterable #2598

Closed
rowillia opened this issue Dec 21, 2016 · 2 comments
Closed

Comments

@rowillia
Copy link
Contributor

rowillia commented Dec 21, 2016

This might be in the category of issues around supporting protocol methods as a whole.

test_iter.py:

from typing import Iterator

class Foo(object):
    def __iter__(self) -> Iterator[int]:
        yield 1
        yield 2

for x in Foo():
    print(x)
$ mypy test_iter.py
test_iter.py:8: error: Iterable expected
@afrieder
Copy link
Contributor

This happens because Iterable is a distinct class, not an interface. To indicate to Python/Mypy that Foo is Iterable, you have to directly subclass Iterable:

from typing import Iterator, Iterable

class Foo(Iterable):
    ....

then the rest of your code correctly type-checks.

@gvanrossum
Copy link
Member

True, but we would all wish that it could work. python/typing#11

I don't think we need to keep this open as a separate issue though.

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

3 participants