Add classproperty helper utility

This is apparently a 3.9+ only addition
This commit is contained in:
Rapptz 2021-05-02 08:05:52 -04:00
parent b0ec22065e
commit cd7357b93a

View File

@ -155,6 +155,15 @@ class CachedSlotProperty(Generic[T, T_co]):
setattr(instance, self.name, value)
return value
class classproperty(Generic[T_co]):
def __init__(self, fget: Callable[[Any], T_co]) -> None:
self.fget = fget
def __get__(self, instance: Optional[Any], owner: Type[Any]) -> T_co:
return self.fget(owner)
def __set__(self, instance, value) -> None:
raise AttributeError('cannot set attribute')
def cached_slot_property(name: str) -> Callable[[Callable[[T], T_co]], CachedSlotProperty[T, T_co]]:
def decorator(func: Callable[[T], T_co]) -> CachedSlotProperty[T, T_co]: