Python ABC seems to allow incomplete implementation

582 views Asked by At

I'm trying to create base class and force all subclasses to implement it's interface. I'm using the abc module for this purpose.

Here is the base class:

class PluginBase:
    __metaclass = abc.ABCMeta
    @abc.abstractmethod
    def strrep(self):
            return
    @abc.abstractmethod
    def idle(self):
            print 'PluginBase: doing nothing here'
            pass
    @abc.abstractmethod
    def say(self, word):
            print 'PluginBase: saying a word ''', word, '\''
            return

And here is the child:

class ConcretePlugin(PluginBase):
    def __init__(self, val):
            print 'initialising ConcretePlugin with value of %d' % val
            self.val = val
    def strrep(self):
            print 'ConcretePlugin = %d' % self.val
            return
    #'idle' method implementation is missing
    def say(self): # missing argument here; saying our own word =)
            print 'ConcretePlugin: this is my word'
            return

This test:

child = ConcretePlugin(307)
child.strrep()
child.idle()
child.say()

produces the following result:

initialising ConcretePlugin with value of 307
ConcretePlugin = 307
PluginBase: doing nothing here
ConcretePlugin: this is my word

No whimpering about incomplete implementation!

So my question is whether Abstract Base Classes are not truly abstract. And if they aren't then is some way to make robust typing?

Note: I've named example classes PluginBase and CompletePlugin to show that I need to make sure that client classes implement the proper interface.

I've tried deriving PluginBase from object, but this makes no difference. I'm using Python 2.7.1

Any help will be appreciated.

1

There are 1 answers

1
ThiefMaster On BEST ANSWER

Change __metaclass to __metaclass__. Otherwise it's just a normal hidden attribute.

>>> ConcretePlugin(123)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class ConcretePlugin with abstract methods idle