Defining a metaclass

  1. class M(type):
  2. pass
  3.  
  4. class A(metaclass=M):
  5. pass

In Python 2, the syntax for defining a metaclass is different:

  1. class A(object):
  2. __metaclass__ = M

Mypy also supports using six.with_metaclass() and @six.add_metaclassto define metaclass in a portable way:

  1. import six
  2.  
  3. class A(six.with_metaclass(M)):
  4. pass
  5.  
  6. @six.add_metaclass(M)
  7. class C(object):
  8. pass