typed_descriptors.prop
Descriptor class for cached properties.
Prop
- class Prop(type, value, /, *, backed_by=None, use_dict=None, use_slots=None, typecheck=True)[source]
Bases:
DescriptorBase[T]A descriptor class for cached properties, supporting:
static type checking for the property value
lazy caching (value only cached at first read)
See
DescriptorBasefor details on how the property value is cached in each instance.- final __get__(instance, _)[source]
If the descriptor is accessed on an instance, returns the value of the property on the given instance.
If the descriptor is accessed on the owner class, i.e. if
instanceisNone, returns thePropobject.
- __init__(type, value, /, *, backed_by=None, use_dict=None, use_slots=None, typecheck=True)[source]
Creates a new property with the given type and value function.
- Parameters:
value (
ValueFunction[T]) – function computing the property valueattr_name – the name of the backing attribute for the property cache, or
Noneto use a default nametypecheck (
bool; default =True) – whether to perform dynamic typechecks (default: True)
- Raises:
- Return type:
- final cache_on(instance)[source]
Caches the property value on the given instance. Can be used to force property computation at a desired time, overriding the default lazy behaviour.
- Raises:
AttributeError – if the property is already cached.
- Parameters:
instance (
Any)- Return type:
- static value(value_fun=None, /, *, backed_by=None, use_dict=None, use_slots=None, typecheck=True)[source]
An alias for
cached_property.It offers a way to declare
Propwhich is stylisticallly aligned to theAttr.validatordecorator for attributes.
- property value_fun
The function called to produce a value for this property on a given instance. It is called by the getter when the property doesn’t have a cached value, and the value returned is automatically cached.
- Return type:
PropFactory
T_co
- T_co = +T_co
Covariant type variable for generic values.
ValueFunction
cached_property
- cached_property(value_fun=None, /, *, backed_by=None, use_dict=None, use_slots=None, typecheck=True)[source]
Decorator used to create a cached property from a value function, optionally specifying a backing attribute. See
PropandDescriptorBasefor more information.It can be used directly, for properties with default backing attribute name:
class C: @cached_property def x(self) -> Sequence[str]: ''' Value function for property 'C.x'. ''' return 10
It can be used by supplying a custom backing attribute name to the
backed_byargument:class C: @cached_property(backed_by="_x") def x(self) -> Sequence[str]: ''' Value function for property 'C.x'. ''' return 10 __slots__ = ("_x", )
Note
The decorator is analogous to the built-in
functools.cached_property, from which it takes its name, and it uses the same caching logic when__dict__is available on owner class’s instances and no custom attribute name is used. Contrary to its built-in counterpart, however, this decorator can be used with a slotted attribute as backing attribute.
validate_value_fun
- validate_value_fun(value_fun, /)[source]
Runtime validation for value functions.
- Raises:
TypeError – if the argument is not a value function
ValueError – if the function doesn’t have an explicit annotation for its return type.
- Parameters:
value_fun (
ValueFunction[T])- Return type:
value_fun_return_type
- value_fun_return_type(value_fun, /)[source]
Returns the return type annotation of a value function. Used by
Propto infer the property type from the value function return type annotation.- Raises:
TypeError – if the argument is not a value function
ValueError – if the function doesn’t have an explicit annotation for its return type.
- Parameters:
value_fun (
ValueFunction[T])- Return type: