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 treats types with __getitem__ as not iterables #11695

Closed
sobolevn opened this issue Dec 9, 2021 · 4 comments
Closed

Mypy treats types with __getitem__ as not iterables #11695

sobolevn opened this issue Dec 9, 2021 · 4 comments
Labels
bug mypy got something wrong

Comments

@sobolevn
Copy link
Member

sobolevn commented Dec 9, 2021

Sample:

class Some:
    def __getitem__(self, i: int) -> str:
        if i >= 2:
            raise IndexError()
        return chr(97 + i)

s = Some()
for x in s:  # error here
    print(x)

Output:

» mypy out/ex.py --strict-optional --warn-unreachable --show-traceback
out/ex.py:10: error: "Some" has no attribute "__iter__" (not iterable)

But, in runtime it works just fine:

» python out/ex.py
a
b

Docs: https://pyneng.readthedocs.io/en/latest/book/23_oop_special_methods/iterable_iterator.html#iteration-protocol

I will try to send a PR soon!

@sobolevn sobolevn added the bug mypy got something wrong label Dec 9, 2021
@JelleZijlstra
Copy link
Member

I'm pretty sure there's already a bug for this. Thanks for working on it!

@sobolevn
Copy link
Member Author

sobolevn commented Dec 9, 2021

Yes: #2220 Found it!

@hauntsaninja
Copy link
Collaborator

@sobolevn want to review #10386 ?

@sobolevn sobolevn closed this as completed Dec 9, 2021
@sobolevn
Copy link
Member Author

sobolevn commented Dec 9, 2021

@hauntsaninja done!

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

No branches or pull requests

3 participants