Skip to content

Instances of classes that define __iter__ = None and __getitem__ should not be iterable. #11220

Closed

Description

@michaelm-openai

Describe the bug
Pyright still allows instances of a class to be iterable if the class defines __getitem__ even if the class also defines __iter__ = None.

Attempting to iterate over an instance of such a class will lead to a runtime TypeError.

Both mypy and ty report this as an error:

Code or Screenshots

class Foo:
    def __getitem__(self, item: int) -> None:
        return

    __iter__ = None

for _ in Foo():
    pass

If your code relies on symbols that are imported from a third-party library, include the associated import statements and specify which versions of those libraries you have installed.

VS Code extension or command-line
As a Node binary - [email protected]

Activity

  1. added
    bug
    on Jan 10, 2026
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions