Skip to content

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

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

Closed
senyai opened this issue Aug 19, 2022 · 1 comment
Closed

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

senyai opened this issue Aug 19, 2022 · 1 comment
Labels
bug mypy got something wrong

Comments

@senyai
Copy link

senyai commented Aug 19, 2022

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.

@senyai senyai added the bug mypy got something wrong label Aug 19, 2022
@tmke8
Copy link
Contributor

tmke8 commented Aug 19, 2022

Duplicate of #2220, I believe.

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Aug 19, 2022
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