Skip to content

Do not support metaclasses not inheriting from type #2820

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

Merged
merged 3 commits into from
Feb 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2024,7 +2024,7 @@ def calculate_metaclass_type(self) -> 'Optional[mypy.types.Instance]':
return None

def is_metaclass(self) -> bool:
return self.has_base('builtins.type')
return self.has_base('builtins.type') or self.fullname() == 'abc.ABCMeta'

def _calculate_is_enum(self) -> bool:
"""
Expand Down
3 changes: 3 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,9 @@ def analyze_metaclass(self, defn: ClassDef) -> None:
if not isinstance(sym.node, TypeInfo) or sym.node.tuple_type is not None:
self.fail("Invalid metaclass '%s'" % defn.metaclass, defn)
return
if not sym.node.is_metaclass():
self.fail("Metaclasses not inheriting from 'type' are not supported", defn)
return
inst = fill_typevars(sym.node)
assert isinstance(inst, Instance)
defn.info.declared_metaclass = inst
Expand Down
14 changes: 11 additions & 3 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -2770,9 +2770,9 @@ class B(A, metaclass=Y): pass # E: Inconsistent metaclass structure for 'B'
class M:
x = 0 # type: int

class A(metaclass=M): pass
class A(metaclass=M): pass # E: Metaclasses not inheriting from 'type' are not supported

reveal_type(A.x) # E: Revealed type is 'builtins.int'
A.x # E: "A" has no attribute "x"
Copy link
Member

@ilevkivskyi ilevkivskyi Feb 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a test that checks that "indirect" inheritance works, like:

class M1(type):
    x = 0
class M2(M1): pass
class C(metaclass=M2):
    pass
reveal_type(C.x) # E: Revealed type is 'builtins.int'

Otherwise LGTM.


[case testMetaclassTypeReveal]
from typing import Type
Expand All @@ -2785,6 +2785,14 @@ def f(TA: Type[A]):
reveal_type(TA) # E: Revealed type is 'Type[__main__.A]'
reveal_type(TA.x) # E: Revealed type is 'builtins.int'

[case testSubclassMetaclass]
class M1(type):
x = 0
class M2(M1): pass
class C(metaclass=M2):
pass
reveal_type(C.x) # E: Revealed type is 'builtins.int'

[case testMetaclassSubclass]
from typing import Type
class M(type):
Expand Down Expand Up @@ -2822,4 +2830,4 @@ from typing import Tuple
class M(Tuple[int]): pass
class C(metaclass=M): pass # E: Invalid metaclass 'M'

[builtins fixtures/tuple.pyi]
[builtins fixtures/tuple.pyi]