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:

  1. class M1(type): pass
  2. class M2(type): pass
  3.  
  4. class A1(metaclass=M1): pass
  5. class A2(metaclass=M2): pass
  6.  
  7. class B1(A1, metaclass=M2): pass # Mypy Error: Inconsistent metaclass structure for 'B1'
  8. # At runtime the above definition raises an exception
  9. # TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
  10.  
  11. # Same runtime error as in B1, but mypy does not catch it yet
  12. 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.