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

Metaclass is not properly inherited when using multiple inheritance with int #2824

Closed
gvanrossum opened this issue Feb 7, 2017 · 12 comments · Fixed by python/typeshed#1598
Closed

Comments

@gvanrossum
Copy link
Member

Consider:

class M(type):
    def foo(self): pass
class C(metaclass=M):
    pass
print(C.__class__)
C.foo()
class D(int, C):
    pass
print(D.__class__)
D.foo()

Running this with Python 3 shows that D's metaclass is M, but when checking this with mypy, we get the error

u.py:10: error: "D" has no attribute "foo"

on the last line, showing that mypy doesn't believe so.

This seems unique to int (or perhaps builtin types?) -- when create a separate class A (without a metaclass) and use that instead of int, 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.

@elazarg
Copy link
Contributor

elazarg commented Feb 7, 2017

It does not reproduce in unit tests.

@elazarg
Copy link
Contributor

elazarg commented Feb 7, 2017

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.

@elazarg
Copy link
Contributor

elazarg commented Feb 7, 2017

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.

@elazarg
Copy link
Contributor

elazarg commented Feb 7, 2017

Well I guess I'm completely wrong. Perhaps it's a simple case of choosing the most derived common metaclass. In typeshed type(int) == ABCMeta. In Python type(int) == type.

@elazarg
Copy link
Contributor

elazarg commented Feb 7, 2017

A possible solution: make EnumMeta inherit ABCMeta, so it will be the most derived common metaclass.

@gvanrossum
Copy link
Member Author

That mostly works, but I needed to add a # type: ignore. Thoughts? https://github.com/python/typeshed/tree/enum-iter

@elazarg
Copy link
Contributor

elazarg commented Feb 9, 2017

Is there some problem with selftype? I have encountered something, but I wasn't sure.

@gvanrossum
Copy link
Member Author

Without that I get

typeshed/stdlib/3.4/enum.pyi:11: error: The erased type of self 'Type[enum.Enum]' is not a supertype of its class 'enum.EnumMeta'

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

t.py:6: error: Revealed type is 't.E*'
t.py:11: error: Revealed type is 't.I*'

which is exactly what it should be.

@elazarg
Copy link
Contributor

elazarg commented Feb 9, 2017

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.

@elazarg
Copy link
Contributor

elazarg commented Feb 9, 2017

As I have mentioned in a different discussion here, it might make sense to use the form cls: Meta[T] which means precisely the type whose metaclass is a subclass of Meta (directly inheriting the generic [] operator from Type). Then we don't need to constrain further T in any way.

@elazarg
Copy link
Contributor

elazarg commented Sep 8, 2017

Root cause: python/typeshed#1595

@JelleZijlstra
Copy link
Member

Recap:

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

Successfully merging a pull request may close this issue.

3 participants