Skip to content

[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

@AlexWaygood

Description

@AlexWaygood

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdiagnosticsRelated to reporting of diagnostics.tyMulti-file analysis & type inference

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions