Resolving metaclass conflicts Resolving metaclass conflicts python python

Resolving metaclass conflicts


Instead of using the receipe as mentioned by jdi, you can directly use:

class M_C(M_A, M_B):    passclass C(A, B):    __metaclass__ = M_C


Your example using sqlite3 is invalid because it is a module and not a class. I have also encountered this issue.

Heres your problem: The base class has a metaclass that is not the same type as the subclass. That is why you get a TypeError.

I used a variation of this activestate snippet using noconflict.py. The snippet needs to be reworked as it is not python 3.x compatible. Regardless, it should give you a general idea.

Problem snippet

class M_A(type):    passclass M_B(type):    passclass A(object):    __metaclass__=M_Aclass B(object):    __metaclass__=M_Bclass C(A,B):    pass#Traceback (most recent call last):#  File "<stdin>", line 1, in ?#TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass #of the metaclasses of all its bases

Solution snippet

from noconflict import classmakerclass C(A,B):    __metaclass__=classmaker()print C#<class 'C'>

The code recipe properly resolves the metaclasses for you.


This also happens when you try to inherit from a function and not a class.

Eg.

def function():    passclass MyClass(function):    pass