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

Mypy doesn't recognize metaclass implementing __getitem__ #1771

Closed
sangsta opened this issue Jun 29, 2016 · 3 comments
Closed

Mypy doesn't recognize metaclass implementing __getitem__ #1771

sangsta opened this issue Jun 29, 2016 · 3 comments

Comments

@sangsta
Copy link

sangsta commented Jun 29, 2016

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.

@gvanrossum
Copy link
Member

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

@gvanrossum
Copy link
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.

@gvanrossum gvanrossum reopened this Feb 6, 2017
gvanrossum pushed a commit that referenced this issue Feb 12, 2017
…#2827)

Fix #1771. (Seems to be much simpler than I thought)

In particular, this will allow removing some special-casing for enum (#2812).

Related to #1381.
@raphCode
Copy link

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants