Python 3 ignores metaclass directive?

175 Views Asked by At

Edited

I have 2 classes inheriting from ABC, and a third class inheriting from both, each in a different file. Tried to provide the metaclass of ABCMeta to the last class, to resolve the conflict of metaclasses, but it fails with the same

"TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases"

Why does python ignore the metaclass directive in this case, and how to resolve it?

file A:

from abc import ABC, abstractmethod

class A(ABC):
    @abstractmethod
    def method1(self):
       pass

file B:

from abc import ABC, abstractmethod

class B(ABC):      
    @abstractmethod
    def method2(self):
       pass

file C:

import A
import B
class C(A,B,metaclass=ABCMeta):
    def method1(self):
       pass
    def method2(self):
       pass
1

There are 1 best solutions below

0
On BEST ANSWER

The problem stems from wrong import. file C should be:

from A import A
from B import B
class C(A,B):
   def method1(self):
      pass
   def method2(self):
      pass

Credit should go to @Matthias & @Giacomo Alzetta, who pointed out that the MCVE works for them.