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 Type error for a class with an imported metaclass #9185

Open
seanp2 opened this issue Jul 21, 2020 · 15 comments
Open

mypy Type error for a class with an imported metaclass #9185

seanp2 opened this issue Jul 21, 2020 · 15 comments
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-metaclasses

Comments

@seanp2
Copy link

seanp2 commented Jul 21, 2020

Please provide more information to help us understand the issue:

  • Are you reporting a bug, or opening a feature request? bug
  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.
    Here is an example class I am working with.
@enum.unique
class MyEnumClass(Enum, metaclass=MyEnumMetaClass):
    OUT_OF_STOCK = "Out of Stock"
    LOW_STOCK = "Low Stock"
    IN_STOCK = "In Stock"
    EXCESS = "Excess"

I am making a change where I am importing the MyEnumMetaClass class from a PIP package now, whereas before it was located within the same project as this class.

  • What is the actual behavior/output?
    With MyEnumMetaClass imported, whenever I create a MyEnumClass instance, mypy gives me the following error:
    error: The type "Type[MyEnumClass]" is not generic and not indexable
  • What is the behavior/output you expect?
    I expect mypy to succeed here, given that the code is the same, it is just imported from a package now.
@ilevkivskyi
Copy link
Member

I think this is a bug, mypy is likely confused by the custom metaclass and doesn't apply the usual enum special-casing. Likely this is easy to fix. The relevant code is in semanal.py (look where is_enum flag on TypeInfo is set).

@ilevkivskyi ilevkivskyi added bug mypy got something wrong false-positive mypy gave an error on correct code good-first-issue priority-1-normal labels Aug 3, 2020
@gvanrossum
Copy link
Member

I am making a change where I am importing the MyEnumMetaClass class from a PIP package now, whereas before it was located within the same project as this class.

This may be key. Is there a stub for that pip package? Or does it have a py.typed marker per PEP 561? If neither is the case, mypy doesn't read the types from the package and assumes the metaclass is Any.

@ilevkivskyi
Copy link
Member

If neither is the case, mypy doesn't read the types from the package and assumes the metaclass is Any.

If the Enum base in the example is the stdlib Enum I think we should apply enum special-casing even if the metaclass is Any.

@Kaushal-Dhungel
Copy link

hey @ilevkivskyi . Seems like this issue is still open. Can I take it??

@ilevkivskyi
Copy link
Member

@Kaushal-Dhungel sure, go head.

@Soumokanti123
Copy link

@Kaushal-Dhungel sure, go head.

Hi sir, Can I take it to solve the problem?

@Hyperion101010
Copy link

Hi there I am Shivam, I know Python to an intermediate level,
I am interested to solve this issue, will you assign me?

@JukkaL
Copy link
Collaborator

JukkaL commented Mar 10, 2021

@Hyperion101010 Done!

@saru-d2
Copy link

saru-d2 commented Mar 28, 2021

Hi, just curious if the issue is being solved/ has been solved, if not I'd love to give it a shot!

@SwagatSBhuyan
Copy link

I'd like to tackle this issue.
@seanp2 if you'd be kind enough to maybe make me reproduce this error, that'd be great! And I also have a few more doubts so if anyone could get back to me that'd be really helpful, as I am new to contributing.
@ilevkivskyi @JukkaL @gvanrossum
Thanks!

@Netaliyev
Copy link

Hi there, I have been trying to contribute to mypy for weeks, but I am intermediate in the Python. I was trying to understand the code in the semanal.py, but it is hard to understand where the problem is. Can you give me tips where should I look so I can try to solve this issue?

@quinnah
Copy link

quinnah commented Nov 7, 2022

Hi there - I'd like to try my hand at this issue. @ilevkivskyi @JukkaL could I claim this issue?

@ilevkivskyi
Copy link
Member

@quinnah Yes, sure, you can give it a try.

@quinnah
Copy link

quinnah commented Nov 18, 2022

Hey @ilevkivskyi, sorry I don't think I'll have time to work on this :/

@ilevkivskyi
Copy link
Member

OK, NP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal topic-metaclasses
Projects
None yet
Development

No branches or pull requests