Type Aliases#
SerializableProperty uses several type aliases to improve code readability and provide clear type hints for callback functions.
Type Variable#
- jangada.serialization.T = TypeVar('T')#
Type variable representing the type of the property value.
Type variable representing the type of the property value.
Callable Type Aliases#
- jangada.serialization.DefaultCallable#
Callable that produces a default value for a property.
- Parameters:
- instanceobject
The instance for which to generate the default value.
- Returns:
- T
The default value.
Callable that produces a default value for a property.
Signature:
(instance: object) -> T- Parameters:
instance (object) – The instance for which to generate the default value
- Returns:
The default value of type T
Example:
def make_default_list(instance: object) -> list: return [] class MyClass: items = SerializableProperty(default=make_default_list)
- jangada.serialization.Observer#
Callable that observes property value changes.
- Parameters:
- instanceobject
The instance whose property changed.
- old_valueT
The previous value of the property.
- new_valueT
The new value of the property.
- Returns:
- None
Callable that observes property value changes.
Signature:
(instance: object, old_value: T, new_value: T) -> None- Parameters:
instance (object) – The instance whose property changed
old_value (T) – The previous value of the property
new_value (T) – The new value of the property
- Returns:
None
Example:
def log_changes(instance: object, old: Any, new: Any) -> None: print(f"Value changed from {old} to {new}") class MyClass: value = SerializableProperty(observers={log_changes})
- jangada.serialization.Parser#
Callable that parses and validates property values.
- Parameters:
- instanceobject
The instance for which to parse the value.
- valueAny
The raw value to parse.
- Returns:
- T
The parsed and validated value.
Callable that parses and validates property values.
Signature:
(instance: object, value: Any) -> T- Parameters:
instance (object) – The instance for which to parse the value
value (Any) – The raw value to parse
- Returns:
The parsed and validated value of type T
- Raises:
Any exception raised during parsing propagates to the caller
Example:
def parse_positive(instance: object, value: Any) -> float: result = float(value) if result <= 0: raise ValueError("Value must be positive") return result class MyClass: temperature = SerializableProperty(parser=parse_positive)
- jangada.serialization.Postinitializer#
Callable that performs post-initialization setup.
- Parameters:
- instanceobject
The instance to initialize.
- Returns:
- None
Callable that performs post-initialization setup.
Signature:
(instance: object) -> None- Parameters:
instance (object) – The instance to initialize
- Returns:
None
- Notes:
The Postinitializer is called once after the property is first set or accessed. It runs after the value has been stored and observers have been called.
Example:
def setup_data(instance: object) -> None: print("Loading expensive data...") if instance.data is None: instance.data = load_from_disk() class MyClass: data = SerializableProperty(postPostinitializer=setup_data)