Gotchas and limitations of metaclass support
Note that metaclasses pose some requirements on the inheritance structure,so it’s better not to combine metaclasses and class hierarchies:
- class M1(type): pass
- class M2(type): pass
- class A1(metaclass=M1): pass
- class A2(metaclass=M2): pass
- class B1(A1, metaclass=M2): pass # Mypy Error: Inconsistent metaclass structure for 'B1'
- # At runtime the above definition raises an exception
- # TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
- # Same runtime error as in B1, but mypy does not catch it yet
- class B12(A1, A2): pass
- Mypy does not understand dynamically-computed metaclasses,such as
class A(metaclass=f()): …
- Mypy does not and cannot understand arbitrary metaclass code.
- Mypy only recognizes subclasses of
type
as potential metaclasses.