craftr.core.property

craftr.core.property.provider

NoValueError Objects

class NoValueError(Exception)

Raised when a provider has no value.

Provider Objects

class Provider(t.Generic[T], metaclass=abc.ABCMeta)

get

 | @abc.abstractmethod
 | get() -> T

Get the value of the propery, or raise a NoValueError.

or_else

 | or_else(default: T) -> T

Get the value of the property, or return default.

or_else_get

 | or_else_get(default_supplier: t.Callable[[], U]) -> t.Union[T, U]

Get the value of the property, or return the value returned by default_supplier.

or_none

 | or_none() -> t.Optional[T]

Get the value of the property, or return None.

craftr.core.property.property

on_property_set_value

on_property_set_value(value_type: t.Any, value: t.Any) -> t.Any

Called when Property.set() is called with not a real value. Allows to transform the value.

Property Objects

class Property(Provider[T])

Properties are mutable providers that sit as attributes on objects of the HavingProperties base class. The type hint of a property is passed into it's construct when the annotation is defined on the HavingProperties class level.

Properties perform extensive runtime type checking when setting a bare value and after evaluating it.

A property can be finalized in which case it's value is calculated and cached. Subsequent calls to get() will return the cached value and attempts to set the property value will result in a RuntimeError.

finalize

 | finalize() -> None

Finalize the property. If already finalized, nothing happens.

HavingProperties Objects

class HavingProperties()

Base for classes that have properties declared as annotations at the class level. Setting property values will automatically wrap them in a Provider if the value is not already one. The constructor will take care of initializing the properties.

collect_properties

collect_properties(provider: t.Union[HavingProperties, Provider]) -> t.List[Property]

Collects all Property objects that are encountered when visiting the specified provider or all properties of a HavingProperties instance.

craftr.core.property.typechecking

Runtime type checking for properties based on type hints.

TypeCheckingContext Objects

class TypeCheckingContext(BaseContext)

type_error

 | type_error(current_value: t.Any, expected_type: t.Any) -> TypeCheckingError

Constructs a TypeError with information from the context.

check_type

check_type(value: t.Any, context: TypeCheckingContext) -> None

Checks if the type of value matches the specified type_hint. The check is performed recursively to exhaustively verify the value type.