[issue34995] functools.cached_property does not maintain the wrapped method's __isabstractmethod__

INADA Naoki report at bugs.python.org
Wed Nov 21 01:24:35 EST 2018


INADA Naoki <songofacandy at gmail.com> added the comment:

My personal opinion is: support abstractmethod only when the descriptor is useful for define interface with ABC.

In case of cached_property, it's not.  It is just a property as interface level.  Caching is just an implementation.

In case of update_wrapper, it's too generic.  A decorated function may be used while define interface, but it's a rare case.  So no need to support it.

In case of singledispatch, I think it is not used in ABC, like cached_property.  But it has shipped in Python already.  We shouldn't remove it easily.

In case of partialmethod... it's considerable. I don't use ABC much, and I never use partialmethod.  So this example is very artificial.

class MyABC(abc.ABC):
    @abstractmethod
    def set_foo(self, v):
        pass
    reset_foo = partialmethod(set_foo, None)

When they subclass of MyABC, they need to override both of `set_foo` and `reset_foo`.  Otherwise, reset_foo is bound to MyABC.set_foo, not subclass' one.
So __isabstractmethod__ support in partialmethod is not just a "commet as a code".

On the other hand, this example can be written as:

class MyABC(abc.ABC):
    @abstractmethod
    def set_foo(self, v):
        pass
    @abstractmethod
    def reset_foo(self):
        pass

Or it can use lazy binding too:

class MyABC(abc.ABC):
    @abstractmethod
    def set_foo(self, v):
        pass

    # No need to override if default implementation is OK
    def reset_foo(self):
        self.set_foo(None)

I am not sure __isabstractmethod__ support in partialmethod is really useful.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34995>
_______________________________________


More information about the Python-bugs-list mailing list