-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Metaclass is not properly inherited when using multiple inheritance with int #2824
Comments
It does not reproduce in unit tests. |
In general, the class/metaclass hierarchy in typeshed does not mirror the hierarchy in Python. I'm not sure, but it could be the reason. Perhaps the special casing of ABCMeta in mypy is also a problem. |
Removing SupportsInt and SupportsFloat from baseclasses makes the error go away. SupportsAbs[int] has nothing to do with the error. Since the formers have ABCMeta as a metaclass, and the latter does not, It points to ABCMeta as the problem. |
Well I guess I'm completely wrong. Perhaps it's a simple case of choosing the most derived common metaclass. In typeshed |
A possible solution: make EnumMeta inherit ABCMeta, so it will be the most derived common metaclass. |
That mostly works, but I needed to add a |
Is there some problem with selftype? I have encountered something, but I wasn't sure. |
Without that I get
FWIW the test program is import enum
class E(enum.Enum):
a = 1
b = 2
for i in E:
reveal_type(i)
class I(enum.IntEnum):
a = 1
b = 2
for k in I:
reveal_type(k) and its output (less the error) is
which is exactly what it should be. |
There are additional checks to add to the subtype visitor that makes this problem go away, but something bothers me. The variable seems to be overly constrained by a class not defined yet. We should add another check for the metaclass declaration, to verify that we are constrained correctly by the type of cls. |
As I have mentioned in a different discussion here, it might make sense to use the form |
Root cause: python/typeshed#1595 |
Recap:
|
Consider:
Running this with Python 3 shows that D's metaclass is M, but when checking this with mypy, we get the error
on the last line, showing that mypy doesn't believe so.
This seems unique to
int
(or perhaps builtin types?) -- when create a separate classA
(without a metaclass) and use that instead ofint
, there is no error.FWIW this prevents me from fixing #2305 (iterating on enums) -- I can get it to work for Enum but not for IntEnum, because it multiply inherits from int and from Enum.
The text was updated successfully, but these errors were encountered: