1

I want abc.abstractmethod to work on the following class:

from abc import ABC
from my_cpp_module import my_class

class A(my_class, ABC):
    @abstractmethod
    def implement_me(self, arg):
        '''not implemented'''

class B(A):
    pass

b = B()                    <--- should throw
b.implement_me('hallo')

But this little example gives me

Traceback (most recent call last):
  File "./abc-test.py", line 4, in <module>
    class A(my_class, ABC):
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

Fiddling a bit with type, classes and so on I get an implementation that runs without error:

class MyABC(type(my_class), ABCMeta): 
    pass  # type(my_class) == <class 'Boost.Python.class'>

class A(my_class, metaclass=MyABC):
    @abstractmethod
    def implement_me(self, arg):
        '''not implemented'''

.. but in this case b = B() just works, too but (of course) I want it to produce the following error:

File "./abc-test.py", line 22, in <module>
    b = B()
TypeError: Can't instantiate abstract class B with abstract methods implement_me

Is there a way to make abstractmethod work together with Boost.Python classes like expected?

frans
  • 8,868
  • 11
  • 58
  • 132
  • Hi frans- working throught the same issue. Haven't come up with an answer, but I believe the problem is with the `__new__` method. Usually, `ABCMeta.__new__` is going to check all members for `__isabstractmethod__`. However, it looks like boost objects contain their own `__new__` method which is somehow covering up the ABC one. – Ben Jones Mar 01 '19 at 16:34

0 Answers0