luminal.interfaces.metadata

Module Contents

Classes

ClassProperty

A decorator that turns a method into a class property.

MetaProperties

A metaclass that enables the ClassProperty decorator to work properly

class ClassProperty

Bases: property

A decorator that turns a method into a class property.

This class inherits from Python’s built-in property class and overrides its __get__() method to allow it to be used as a class property.

Examples

To use ClassProperty, simply decorate a method with it:

>>> class MyClass:
...     _prop = 'value'
...
...     @ClassProperty
...     def prop(cls):
...         return cls._prop
...
>>> MyClass.prop
'value'
__get__(*args, **_)

Get the value of the class property for a given object or class.

Returns:

The value of the class property.

Return type:

object

Raises:

AttributeError – If the property is accessed on an object that does not have an attribute with the same name as the property.

Parameters:

args (tuple) –

__set__(cls_or_instance, value)

Set the value of the class property.

This method is called when the class property is set on the class or an instance of the class. However, because this is a class property, this method will only be called when the property is set by the class itself, and is not intended for external use; or even by private methods.

Parameters:
  • cls_or_instance (object) – The class that the property is being set on.

  • value (object) – The new value of the class property.

Raises:

AttributeError – When attempting to set the class property on an instance of the class rather than the class itself.

Return type:

None

__delete__(cls_or_instance)

Delete the class property from the class.

Parameters:

cls_or_instance (object) – The class from which the class property is being deleted.

Raises:

AttributeError – When attempting to delete the class property from an instance of the class rather than the class itself.

Return type:

None

_get_class(cls_or_instance)

Get the class of an object, or return the class itself if given.

Parameters:
  • cls_instance (Type[object|type]) – The object or type whose class should be retrieved.

  • cls_or_instance (Type[object | type]) –

Returns:

  • type – The class of the object if one is provided, or the class itself if a class is provided.

  • Raises

  • ———-

  • TypeError – An exception if cls_or_instance is not a valid object or type.

Return type:

type

class MetaProperties

Bases: type

A metaclass that enables the ClassProperty decorator to work properly with class-level operations like setting and deleting properties.

When using ClassProperty to create class properties, the containing class must use this metaclass in order for the property to work properly with class-level operations like setting and deleting the property.

For example, to use ClassProperty to define a read-only class property:

>>> class MyClass(metaclass=MetaProperties):
...     @ClassProperty
...     def prop(cls):
...         return "value"
...
>>> MyClass.prop # Get the value of the property.
'value'
>>> MyClass.prop = "new value" # Attempt to set the value of the property.
AttributeError: can't set attribute
>>> del MyClass.prop # Attempt to delete the property.
AttributeError: can't delete attribute

Note that attempting to set or delete a ClassProperty object will raise an AttributeError by default, unless the __set__() and/or __delete__() methods are defined on the ClassProperty object.

See also

ClassProperty

The decorator used to create class properties.

__setattr__(name, value)

Override the __setattr__() method to allow setting class properties.

This method is called when an attribute of the class is set. If the attribute is a ClassProperty instance (as determined by the presence of a __set__() method on the instance), it will call the __set__() method to set the property’s value. Otherwise, it will invoke the superclass implementation of __setattr__() to handle the attribute in the usual way.

Parameters:
  • name (str) – The name of the attribute that’s being set.

  • value (object) – The new value of the attribute.

Raises:

AttributeError – If name refers to a ClassProperty object and the __set__() method of that object raises an AttributeError.

Return type:

None

__delattr__(name)

Override the __delattr__() method to allow deleting of class properties.

Parameters:

name (str) – The name of the attribute that’s being deleted.

Raises:

AttributeError – If name refers to a ClassProperty object and the __delete__() method of that object raises an AttributeError.

Notes

  • This method is called when an attribute of the class is deleted. If the attribute is a ClassProperty instance (as determined by the presence of a __delete__() method on the instance), it will call the __delete__() method to delete the property. Otherwise, it will invoke the superclass implementation of __delattr__() to handle the attribute in the usual way.

_get_attribute(name)

Retrieves a ClassProperty attribute with the given name from this MetaProperties instance.

Parameters:

name (str) – The name of the attribute to retrieve.

Returns:

The ClassProperty object for the attribute, if it exists. Otherwise, returns None.

Return type:

Optional[ClassProperty]