Skip to content

Classes that implement getitem are reported as incompatible with enumerate #9737

Closed
@scuac

Description

@scuac

Bug Report

Mypy reports erroneously that a class that implements getitem is not suitable to be used with enumerate.

To Reproduce

Small example:

class A:
    def __init__(self):
        self.data = [1, 2, 3]

    def __len__(self):
        return len(self.data)

    def __getitem__(self, item):
        return self.data[item]


a = A()
for i, item in enumerate(a):
    print(f'item {i} = {item}')

Expected Behavior

Running mypy on the above code should report no errors

Actual Behavior

Mypy reported:

error: Argument 1 to "enumerate" has incompatible type "A"; expected "Iterable[<nothing>]"

Your Environment

  • Mypy version used: 0.790
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files):
[mypy]
ignore_missing_imports = True
  • Python version used: 3.7.6
  • Operating system and version: macOS 10.15.7

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