Jump to >

This documentation covers Djblets 2.x. You can see the latest Djblets documentation or all other versions.

djblets.util.properties

Specialized descriptors/properties for classes.

class BaseProperty[source]

Base class for a custom property for a class.

This is an optional base class that provides handy utilities that properties may need. For instance, being able to determine the name of the property’s attribute on a class.

get_attr_name(instance)[source]

Return the name of this property’s attribute.

The value will be computed only once per property instance.

Parameters

instance (object) – The instance owning this property.

Returns

The name of this property on the instance.

Return type

str

class AliasProperty(prop_name, convert_to_func=None, convert_from_func=None, deprecated=False, deprecation_warning=<class 'DeprecationWarning'>)[source]

A property that aliases to another property or attribute.

Alias properties are used to automatically retrieve from another property on access, or to set a value on another property. It’s useful when wanting to rename an attribute but continue to provide a deprecated version, or when creating an object that provides a set of compatibility attributes for use with legacy code.

Alias properties can optionally emit a deprecation warning on use, in order to help in the process of migrating legacy code.

class TypedProperty(valid_types, default=None, allow_none=True)[source]

A property that enforces type safety.

This property will ensure that only values that are compatible with a given type can be set. This ensures type safety and helps catch errors early.

get_descriptor_attr_name(descriptor, cls)[source]

Return the name of a property/descriptor instance on a class.

This will go through the class and all parent classes, looking for the property, and returning its attribute name. This is primarily intended to help with providing better error messages.

Parameters
  • descriptor (object) – The instance of the property/descriptor. For a proper value to be returned, this must exist on cls.

  • cls (type) – The class owning the property.

Returns

The name of the property/descriptor.

Return type

str