Skip to content

enumerate doesn't understand classes with __len__ and __getitem__ #13451

Closed as not planned
@senyai

Description

@senyai

Bug Report

Hi! This is my first bug report. Let's start with an example:

class Iter:
    def __len__(self) -> int:
        return 2
    def __getitem__(self, idx: int) -> str:
        return 'AB'[idx]


def foo() -> None:
    for i, text in enumerate(Iter()):
        reveal_type(text)
        pass

foo()
python -m mypy ./xxx.py
xxx.py:9: error: Need type annotation for "text"
xxx.py:9: error: Argument 1 to "enumerate" has incompatible type "Iter"; expected "Iterable[<nothing>]"
xxx.py:10: note: Revealed type is "Any"

I don't expect text to be Any.

this is mypy 0.971

Yes, I can add __iter__, understand what Iterable[<nothing>] mean and fix the issue on my side, BUT! I found this trying to use QPolygonF class from PyQt5 that is defined exactly like this and this is perfectly valid python, so somehow I'm sure something must be done on mypy side.

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