Author dsdale24
Recipients dsdale24
Date 2011-03-19.18:49:09
SpamBayes Score 1.23166e-10
Marked as misclassified No
Message-id <1300560551.62.0.116291646024.issue11610@psf.upfronthosting.co.za>
In-reply-to
Content
I posted a suggestion at python-ideas that the declaration of abstract properties could be improved in such a way that they could be declared with either the long-form or decorator syntax using the built-in property and abc.abstractmethod:

{{{
class MyProperty(property):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for f in (self.fget, self.fset, self.fdel):
            if getattr(f, '__isabstractmethod__', False):
                self.__isabstractmethod__ = True
                break

class C(metaclass=ABCMeta):
    @MyProperty
    @abstractmethod
    def x(self):
        pass
    @x.setter
    @abstractmethod
    def x(self, val):
        pass

    # this syntax would also be supported:
    #@abstractmethod
    #def getx(self):
    #    pass
    #@abstractmethod
    #def setx(self, val):
    #    pass
    #x = MyProperty(getx, setx)

class D(C):
    'D does not define a concrete setter and cannot be instantiated'
    @C.x.setter
    def x(self):
        return 1

class E(D):
    'E has a concrete getter and setter, and can be instantiated'
    @D.x.setter
    def x(self, val):
        pass
}}}

It is hopefully evident that a relatively minor extension can be made to the built-in property such that @abstractproperty would no longer be needed. I have prepared a patch, complete with documentation and unit tests, but unfortunately I have not been able to test it because I have not been able to build Python from a mercurial checkout on either Ubuntu 11.04 or OS X 10.6.6 (for reasons unrelated to the patch.) BDFL thought the idea sounded good for inclusion in Python-3.3, and requested I submit the patch here.
History
Date User Action Args
2011-03-19 18:49:11dsdale24setrecipients: + dsdale24
2011-03-19 18:49:11dsdale24setmessageid: <1300560551.62.0.116291646024.issue11610@psf.upfronthosting.co.za>
2011-03-19 18:49:11dsdale24linkissue11610 messages
2011-03-19 18:49:10dsdale24create