How to create an abstract cached property in Python?

Viewed 167

In order to create an abstract property in Python one can use the following code:

from abc import ABC, abstractmethod

class AbstractClassName(ABC):

    @cached_property
    @abstractmethod
    def property_name(self) -> str:
        pass


class ClassName(AbstractClassName):

    @property
    def property_name(self) -> str:
        return 'XYZ'


>>> o = AbstractClassName()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class AbstractClassName with abstract method property_name
>>> o = ClassName()
>>> o.property_name
'XYZ'

which is what I have expected. I wanted to create an abstract cached property, so I tried the following:

from abc import ABC, abstractmethod
from functools import cached_property


class AbstractClassName(ABC):

    @cached_property
    @abstractmethod
    def property_name(self) -> str:
        pass


class ClassName(AbstractClassName):

    @cached_property
    def property_name(self) -> str:
        return 'XYZ'

However, this is not working as I expected:

>>> o = AbstractClassName()
>>> o.property_name
>>> o = ClassName()
>>> o.property_name
'XYZ'

Notice that this time it is allowing me to create an instance of an abstract class AbstractClassName. I am using Python 3.10. Is there any way to defined an abstract cached property?

0 Answers
Related