"""A wrapper of cached_property that also saves which things are cached."""fromfunctoolsimportcached_propertyascp
[docs]classcached_property(cp):# noqadef__get__(self,obj,cls):"""Get the store value of the attribute."""try:value=super().__get__(obj,cls)exceptAttributeErrorase:raiseRuntimeError(f"{self.func.__name__} failed with an AttributeError")frome# Add the name of the decorated func to the _cached_ item of the dictifobjisnotNone:if"_cached_"notinobj.__dict__:obj.__dict__["_cached_"]=set()obj.__dict__["_cached_"].add(self.func.__name__)returnvalue
[docs]defsafe_property(f):"""An alternative property decorator that substitates AttributeError for RunTime."""defgetter(*args,**kwargs):try:returnf(*args,**kwargs)exceptAttributeErrorase:raiseRuntimeError(f"Wrapped AttributeError in {f.__name__}")fromereturnproperty(getter)