Skip to content

Mypy doesn't recognize metaclass implementing __getitem__ #1771

Closed

Description

@sangsta

Suppose we have an Enum called MyEnum, and we try something like MyEnum["foo"], mypy will give us an error Value of type "MyEnum" is not indexable. We believe this is because mypy has trouble recognizing when a metaclass implements __getitem__.

A workaround is to do getattr(MyEnum, "foo"), but it'd be nice to have an actual fix.

Activity

  1. gvanrossum commented on Jun 29, 2016

    @gvanrossum
    Member

    Oh, it looks like this already exists in the tracker: #741. I'll have to close this as a duplicate.

  2. gvanrossum commented on Feb 6, 2017

    @gvanrossum
    Member

    Reopening this, since there's an extra wrinkle specifically for __getitem__ -- because X[Y] is used as generic type parameterization, even with @elazarg's fix (#2475) the metaclass's __getitem__ is not honored.

  3. added a commit that references this issue on Feb 12, 2017
    e0e9c2e
  4. raphCode commented on Apr 27, 2023

    @raphCode

    Sorry to dig this up after years, but it seems the issue was never really resolved besides printing the correct type in reveal_type().
    I opened a new issue with details: #15107

    I spent quite some time struggling to get custom generics to work in mypy, because this issue and the associated PR made me believe the feature is implemented.
    To anyone in the same situation:
    Using custom generics via metaclass __getitem__ is not supported in mypy at the time being.

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions