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

STINNER Victor report at bugs.python.org
Thu Oct 25 17:04:37 EDT 2018


STINNER Victor <vstinner at redhat.com> added the comment:

Exmaple 1:
---
import abc
import functools

class AbstractExpensiveCalculator(abc.ABC):
    @abc.abstractmethod
    @functools.cached_property
    def calculate(self):
        pass

AbstractExpensiveCalculator()
---

Exmaple 2:
---
import abc
import functools

class AbstractExpensiveCalculator(abc.ABC):
    @functools.cached_property
    @abc.abstractmethod
    def calculate(self):
        pass

AbstractExpensiveCalculator()
---

Current behavior: Example 1 raises an exception as expected, Example 2 instanciate the object: no exception is raised.

PR 9904 looks like a reasonable change to me.


> The cached_property decorator is not inherited by overriding properties. I don't think that combining the cached_property and the @abstractmethod decorators should be supported.

Well, maybe we can hack something to make Example 2 fail as well, but I like the idea of using @functools.cached_property in an abstract class as "documentation". To announce that the property will be cached, even if technically it will not be cached. It's more to use the code as documentation than to execute any code.

PR 9904 is just 4 lines of code to make the code "works as expected".

----------
nosy: +vstinner

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


More information about the Python-bugs-list mailing list