Skip to content

Mypy treats types with __getitem__ as not iterables #11695

Closed

Description

@sobolevn

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!

Activity

  1. JelleZijlstra commented on Dec 9, 2021

    @JelleZijlstra
    Member

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

  2. sobolevn commented on Dec 9, 2021

    @sobolevn
    MemberAuthor

    Yes: #2220 Found it!

  3. hauntsaninja commented on Dec 9, 2021

    @hauntsaninja
    Collaborator

    @sobolevn want to review #10386 ?

  4. sobolevn commented on Dec 9, 2021

    @sobolevn
    MemberAuthor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions