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

[red-knot] Incorrect error message when iterating over an object that has an __iter__ method that might not return an object with a __next__ method #16272

Open
AlexWaygood opened this issue Feb 20, 2025 · 0 comments · May be fixed by #16321
Labels
bug Something isn't working diagnostics Related to reporting of diagnostics. red-knot Multi-file analysis & type inference

Comments

@AlexWaygood
Copy link
Member

AlexWaygood commented Feb 20, 2025

Description

For this snippet:

class Iterator:
    def __next__(self) -> int:
        return 42

class Iterable:
    def __iter__(self) -> Iterator | int:
        return Iterator()

for x in Iterable():
    pass

We currently emit this diagnostic:

error: lint:not-iterable
  --> /Users/alexw/dev/experiment/foo.py:9:10
   |
 7 |         return Iterator()
 8 |
 9 | for x in Iterable():
   |          ^^^^^^ Object of type `Iterable` is not iterable because its `__iter__` method is possibly unbound
10 |     pass
   |

It's correct for us to emit a diagnostic here. However, this is an incorrect error message: __iter__ here is definitely bound, but it returns an object that might not necessarily have a __next__ method.

@AlexWaygood AlexWaygood added bug Something isn't working red-knot Multi-file analysis & type inference diagnostics Related to reporting of diagnostics. labels Feb 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working diagnostics Related to reporting of diagnostics. red-knot Multi-file analysis & type inference
Projects
None yet
1 participant