typed_descriptors.base
Abstract base class for descriptors backed by protected/private attributes.
DescriptorBase
- class DescriptorBase(type, /, *, backed_by=None, use_dict=None, use_slots=None)[source]
Bases:
TypedDescriptor[T]Base class for descriptors backed by an attribute whose name and access mode is determined by the following logic.
Logic for using
__dict__vs “attr” functions for access to the backing attribute:If the
use_dictargument is set toTruein the descriptor constructor, then__dict__will be used. If the library is certain that__dict__is not available on instances of the descriptor owner class (cf.is_dict_available), then aTypeErroris raised at the time when__set_name__is called.If the
use_slotsargument is set toTruein the descriptor constructor, then the “attr” functionsgetattr,setattr,delattrandhasattrwill be used. If the library is certain that__dict__is not available on instances of the descriptor owner class (cf.is_dict_available) and the backing attribute name is not present in the class slots (cf.class_slots), then aTypeErroris raised at the time when__set_name__is called.If neither
use_dictnoruse_slots__is set toTruein the descriptor constructor (the default case), thenis_dict_availableis called and the result is used to determine whether to use__dict__or slots for the backing attribute. Further validation is then performed, as described in points 1 and 2 above.
Naming logic for the backing attribute:
If the
backed_byargument is specified in the descriptor constructor, the string passed to it is used as name for the backing attribute.Else, if using
__dict__for access to the backing attribute, then the backing attribute name coincides with the descriptor name.Else, the backing attribute name is obtained by prepending one or two underscores to the descriptor name (one if the descriptor name starts with underscore, two if it doesn’t).
If the backing attribute name starts with two underscores but does not end with two underscores, name-mangling is automatically performed.
- abstract __get__(instance, _)[source]
If the descriptor is accessed on an instance, returns the value of the descriptor on the given instance.
If the descriptor is accessed on the owner class, i.e. if
instanceisNone, returns the descriptor object.
- __init__(type, /, *, backed_by=None, use_dict=None, use_slots=None)[source]
Creates a new descriptor with the given type and optional validator.
- Parameters:
backed_by (
Optional[str]) – name for the backing attribute (optional, default name used if not specified).use_dict (
Optional[Literal[True]]) – if set toTrue,__dict__will be used to store the the backing attribute.use_dict – if set to
True,__slots__will be used to store the the backing attribute.
- Raises:
TypeError – if the type cannot be validated by the
typing_validationlibrary.- Return type:
- final __set_name__(owner, name)[source]
Hook called when the descriptor is assigned to a class attribute. Responsible for:
Setting the owner and name of the descriptor
Setting the name of the backing attribute (incl. name-mangling)
Determining how to access the backing attribute.
See
DescriptorBaseclass documentation for the logic behind the backing attribute’s name and access mode.- Raises:
- Parameters:
- Return type:
- property is_assigned
Whether the descriptor has been assigned its owner and name.
- Return type:
T
- T = ~T
Invariant type variable for generic values.
T_co
- T_co = +T_co
Invariant type variable for generic values.
TypedDescriptor
- class TypedDescriptor(*args, **kwargs)[source]
-
Structural type for typed descriptors.
- __descriptor_type__
Indicates the type (or type annotation, if a string) for the descriptor.
- Return type:
class_slots
is_dict_available
- is_dict_available(owner)[source]
Checks whether instances of a descriptor owner class have
__dict__. ReturnsTrueif the MRO rootowner.__mro__[-1]is notobject, or if the following is true for any classclsinowner.__mro__[:-1](i.e. excluding the MRO root):clsdoes not define__slots__, or__dict__appears in the__slots__forcls