Skip to content

Commit

Permalink
paquo.utils: fix ro cached_property implementation for py36/py37
Browse files Browse the repository at this point in the history
  • Loading branch information
ap-- committed Aug 20, 2020
1 parent 1ec84f7 commit a1e3ba3
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions paquo/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,27 @@ def __set__(self, obj, value):
except ImportError:
# noinspection PyPep8Naming
class cached_property: # type: ignore # https://github.com/python/mypy/issues/1153
def __init__(self, getter):
self.getter = getter
self.name = getter.__name__
_NOCACHE = object()

def __init__(self, fget):
self.fget = fget
self.attrname = None
self.__doc__ = fget.__doc__

def __set_name__(self, owner, name):
self.attrname = name

def __get__(self, obj, objtype=None):
if obj is None:
return self # pragma: no cover
value = obj.__dict__[self.name] = self.getter(obj)
return value
cache = obj.__dict__
val = cache.get(self.attrname, self._NOCACHE)
if val is self._NOCACHE:
val = cache[self.attrname] = self.fget(obj)
return val

# def __set__(self, obj, value):
# raise AttributeError(f"readonly attribute {self.name}")
def __set__(self, obj, value):
raise AttributeError(f"readonly attribute {self.fget.__name__}")

try:
from contextlib import nullcontext # type: ignore
Expand Down

0 comments on commit a1e3ba3

Please sign in to comment.