38

How do I create a decorator for an abstract class method in Python 2.7?

Yes, this is similar to this question, except I would like to combine abc.abstractmethod and classmethod, instead of staticmethod. Also, it looks like abc.abstractclassmethod was added in Python 3 (I think?), but I'm using Google App Engine, so I'm currently limited to Python 2.7

Thanks in advance.

Community
  • 1
  • 1
jchu
  • 685
  • 1
  • 5
  • 13
  • If you can't use abc.abstractclassmethod, then how does the issue of how to combine it with another decorator even arise? – abarnert Jun 27 '12 at 01:24
  • 3
    Think you misread: abc.abstractclassmethod = abc.abstractmethod + classmethod. Python 2.7 has abstractmethod and classmethod, but not abstractclassmethod – jchu Jun 27 '12 at 20:05
  • 1
    I am facing exactly similar problem! Good question! – Karthick Jun 28 '12 at 15:21

4 Answers4

30

Here's a working example derived from the source code in Python 3.3's abc module:

from abc import ABCMeta

class abstractclassmethod(classmethod):

    __isabstractmethod__ = True

    def __init__(self, callable):
        callable.__isabstractmethod__ = True
        super(abstractclassmethod, self).__init__(callable)

class DemoABC:

    __metaclass__ = ABCMeta

    @abstractclassmethod
    def from_int(cls, n):
        return cls()

class DemoConcrete(DemoABC):

    @classmethod
    def from_int(cls, n):
        return cls(2*n)

    def __init__(self, n):
        print 'Initializing with', n

Here's what it looks like when running:

>>> d = DemoConcrete(5)             # Succeeds by calling a concrete __init__()
Initializing with 5

>>> d = DemoConcrete.from_int(5)    # Succeeds by calling a concrete from_int()
Initializing with 10

>>> DemoABC()                       # Fails because from_int() is abstract    
Traceback (most recent call last):
  ...
TypeError: Can't instantiate abstract class DemoABC with abstract methods from_int

>>> DemoABC.from_int(5)             # Fails because from_int() is not implemented
Traceback (most recent call last):
  ...
TypeError: Can't instantiate abstract class DemoABC with abstract methods from_int

Note that the final example fails because cls() won't instantiate. ABCMeta prevents premature instantiation of classes that haven't defined all of the required abstract methods.

Another way to trigger a failure when the from_int() abstract class method is called is to have it raise an exception:

class DemoABC:

    __metaclass__ = ABCMeta

    @abstractclassmethod
    def from_int(cls, n):
        raise NotImplementedError

The design ABCMeta makes no effort to prevent any abstract method from being called on an uninstantiated class, so it is up to you to trigger a failure by invoking cls() as classmethods usually do or by raising a NotImplementedError. Either way, you get a nice, clean failure.

It is probably tempting to write a descriptor to intercept a direct call to an abstract class method, but that would be at odds with the overall design of ABCMeta (which is all about checking for required methods prior to instantiation rather than when methods are called).

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
  • 2
    Good idea to check the Python source code, but unfortunately, similar to when I write `classmethod`, followed by `abc.abstractmethod`, this does not enforce `abstractmethod`. i.e. I can still call the method. – jchu Jun 27 '12 at 20:43
  • 2
    @jchu ABCMeta is about preventing class instantiation when abstract methods are missing. It has no code to prevent calls to abstract methods on an uninstantiated class. If you want a clean failure on a call to an abstract class method, then have it raise *NotImplementedError* or have it attempt to instantiate with ``cls()``. – Raymond Hettinger Aug 22 '14 at 07:17
  • So it's a common Python pattern to instantiate an object from a classmethod? For my particular use case, it does not make sense to instantiate an object from within the classmethod, which perhaps explains my disconnect. I ended up raising a NotImplementedError in the abstract base class, as you mentioned in your alternate option. But in that case, is there any difference in behavior if you used the abstractclassmethod decorator or the classmethod decorator (besides clarity in intent)? – jchu Aug 24 '14 at 07:20
  • Yes, the principal use case for a classmethod is to provide alternate constructors, such as *datetime.now()* or *dict.fromkeys()*. And yes, there is a difference between *abstractclassmethod* and a plain *classmethod*. Besides being more clear in intent, a missing *abstractclassmethod* will prevent instantiation of the class even will the normal constructor, see the third-example ``DemoABC()`` above. The *ABCMeta* metaclass check the entire ``todo-list`` of abstractmethods before instantiating. – Raymond Hettinger Aug 24 '14 at 16:34
  • 1
    Gotcha. It seems the intent of abstractclassmethod was different than how I was thinking about it. Thanks for clarifying. – jchu Aug 25 '14 at 18:37
  • This doesn't seem to enforce implementation as a class method on the concrete class, which I took to be the point of the question. I can replace `DemoConcrete.from_int()` with `def from_int(self, n): return n` without any `@classmethod` decorator, and `DemoConcrete.from_int()` still runs just fine. – Dave Kielpinski May 02 '17 at 20:43
21

You could upgrade to Python 3.

Starting with Python 3.3, it is possible to combine @classmethod and @abstractmethod:

import abc
class Foo(abc.ABC):
    @classmethod
    @abc.abstractmethod
    def my_abstract_classmethod(...):
        pass

Thanks to @gerrit for pointing this out to me.

Neuron
  • 5,141
  • 5
  • 38
  • 59
cowlinator
  • 7,195
  • 6
  • 41
  • 61
20

Another possible workaround:

class A:
    __metaclass__ = abc.ABCMeta
    
    @abc.abstractmethod
    def some_classmethod(cls):
        """IMPORTANT: this is a class method, override it with @classmethod!"""

class B(A):
    @classmethod
    def some_classmethod(cls):
        print cls

Now, one still can't instantiate from A until some_classmethod is implemented, and it works if you implement it with a @classmethod.

Neuron
  • 5,141
  • 5
  • 38
  • 59
dmytro
  • 1,293
  • 9
  • 21
2

I recently encountered the same problem. That is, I needed abstract classmethods but was unable to use Python 3 because of other project constraints. The solution I came up with is the following.

abc-extend.py:

import abc

class instancemethodwrapper(object):
    def __init__(self, callable):
        self.callable = callable
        self.__dontcall__ = False

    def __getattr__(self, key):
        return getattr(self.callable, key)

    def __call__(self, *args, **kwargs):
        if self.__dontcall__:
            raise TypeError('Attempted to call abstract method.')
        return self.callable(*args,**kwargs)

class newclassmethod(classmethod):
    def __init__(self, func):
        super(newclassmethod, self).__init__(func)
        isabstractmethod = getattr(func,'__isabstractmethod__',False)
        if isabstractmethod:
            self.__isabstractmethod__ = isabstractmethod
        
    def __get__(self, instance, owner):
        result = instancemethodwrapper(super(newclassmethod, self).__get__(instance, owner))
        isabstractmethod = getattr(self,'__isabstractmethod__',False)
        if isabstractmethod:
            result.__isabstractmethod__ = isabstractmethod
            abstractmethods = getattr(owner,'__abstractmethods__',None)
            if abstractmethods and result.__name__ in abstractmethods:
                result.__dontcall__ = True
        return result

class abstractclassmethod(newclassmethod):
    def __init__(self, func):
        func = abc.abstractmethod(func)
        super(abstractclassmethod,self).__init__(func)

Usage:

from abc-extend import abstractclassmethod

class A(object):
    __metaclass__ = abc.ABCMeta    
    @abstractclassmethod
    def foo(cls):
        return 6

class B(A):
    pass
    
class C(B):
    @classmethod
    def foo(cls):
        return super(C,cls).foo() + 1

try:
    a = A()
except TypeError:
    print 'Instantiating A raises a TypeError.'

try:
    A.foo()
except TypeError:
    print 'Calling A.foo raises a TypeError.'

try:
    b = B()
except TypeError:
    print 'Instantiating B also raises a TypeError because foo was not overridden.'

try:
    B.foo()
except TypeError:
    print 'As does calling B.foo.'

#But C can be instantiated because C overrides foo
c = C()

#And C.foo can be called
print C.foo()

And here are some pyunit tests which give a more exhaustive demonstration.

test-abc-extend.py:

import unittest
import abc
oldclassmethod = classmethod
from abc-extend import newclassmethod as classmethod, abstractclassmethod

class Test(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def testClassmethod(self):
        class A(object):
            __metaclass__ = abc.ABCMeta            
            @classmethod
            @abc.abstractmethod
            def foo(cls):
                return 6
            
        class B(A):
            @classmethod
            def bar(cls):
                return 5
        
        class C(B):
            @classmethod
            def foo(cls):
                return super(C,cls).foo() + 1
        
        self.assertRaises(TypeError,A.foo)
        self.assertRaises(TypeError,A)
        self.assertRaises(TypeError,B)
        self.assertRaises(TypeError,B.foo)
        self.assertEqual(B.bar(),5)
        self.assertEqual(C.bar(),5)
        self.assertEqual(C.foo(),7)
        
    def testAbstractclassmethod(self):
        class A(object):
            __metaclass__ = abc.ABCMeta    
            @abstractclassmethod
            def foo(cls):
                return 6

        class B(A):
            pass
        
        class C(B):
            @oldclassmethod
            def foo(cls):
                return super(C,cls).foo() + 1
                    
        self.assertRaises(TypeError,A.foo)
        self.assertRaises(TypeError,A)
        self.assertRaises(TypeError,B)
        self.assertRaises(TypeError,B.foo)
        self.assertEqual(C.foo(),7)
        c = C()
        self.assertEqual(c.foo(),7)

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()

I haven't evaluated the performance cost of this solution, but it has worked for my purposes so far.

Neuron
  • 5,141
  • 5
  • 38
  • 59
jcrudy
  • 3,921
  • 1
  • 24
  • 31