betty.serde.load module

Provide a deserialization API.

class betty.serde.load.Asserter[source]

Bases: object

Provide deserialization assertions.

assert_assertions(assertions: betty.serde.load._Assertions[betty.serde.load.ValueT, betty.serde.load.ReturnT, Any]) Callable[[Any], betty.serde.load.ReturnT][source]

Assert that an assertions chain passes, and return the chain’s output.

Parameters:

assertions (betty.serde.load._Assertions[typing.TypeVar(ValueT), typing.TypeVar(ReturnT), typing.Any])

Return type:

typing.Callable[[typing.Any], typing.TypeVar(ReturnT)]

assert_bool() Callable[[Any], bool][source]

Assert that a value is a Python bool.

Return type:

typing.Callable[[typing.Any], bool]

assert_dict() Callable[[Any], dict[str, Any]][source]

Assert that a value is a Python dict.

Return type:

typing.Callable[[typing.Any], dict[str, typing.Any]]

assert_directory_path() Callable[[Any], pathlib.Path][source]

Assert that a value is a path to an existing directory.

Return type:

typing.Callable[[typing.Any], pathlib.Path]

assert_entity_type() Callable[[Any], type[betty.model.Entity]][source]

Assert that a value is an entity type.

This assertion passes if the value is fully qualified betty.model.Entity subclass name.

Return type:

typing.Callable[[typing.Any], type[betty.model.Entity]]

assert_extension_type() Callable[[Any], type[betty.app.extension.Extension]][source]

Assert that a value is an extension type.

This assertion passes if the value is fully qualified betty.app.extension.Extension subclass name.

Return type:

typing.Callable[[typing.Any], type[betty.app.extension.Extension]]

assert_field(field: betty.serde.load._Field[betty.serde.load.ValueT, betty.serde.load.ReturnT]) Callable[[betty.serde.load.ValueT], betty.serde.load.ReturnT | type[betty.serde.dump.Void]][source]

Assert that a value is a key-value mapping of arbitrary value types, and assert a single of its values.

Parameters:

field (betty.serde.load._Field[typing.TypeVar(ValueT), typing.TypeVar(ReturnT)])

Return type:

typing.Callable[[typing.TypeVar(ValueT)], typing.Union[typing.TypeVar(ReturnT), type[betty.serde.dump.Void]]]

assert_fields(fields: betty.serde.load.Fields) Callable[[Any], MutableMapping[str, Any]][source]

Assert that a value is a key-value mapping of arbitrary value types, and assert several of its values.

Parameters:

fields (betty.serde.load.Fields)

Return type:

typing.Callable[[typing.Any], typing.MutableMapping[str, typing.Any]]

assert_float() Callable[[Any], float][source]

Assert that a value is a Python float.

Return type:

typing.Callable[[typing.Any], float]

assert_int() Callable[[Any], int][source]

Assert that a value is a Python int.

Return type:

typing.Callable[[typing.Any], int]

assert_list() Callable[[Any], list[Any]][source]

Assert that a value is a Python list.

Return type:

typing.Callable[[typing.Any], list[typing.Any]]

assert_locale() Callable[[Any], str][source]

Assert that a value is a valid IETF BCP 47 language tag.

Return type:

typing.Callable[[typing.Any], str]

assert_mapping(item_assertion: betty.serde.load.Assertions[betty.serde.load.ValueT, betty.serde.load.ReturnT]) Callable[[Any], MutableMapping[str, betty.serde.load.ReturnT]][source]

Assert that a value is a key-value mapping and assert that all item values are of the given type.

Parameters:

item_assertion (betty.serde.load.Assertions[typing.TypeVar(ValueT), typing.TypeVar(ReturnT)])

Return type:

typing.Callable[[typing.Any], typing.MutableMapping[str, typing.TypeVar(ReturnT)]]

assert_none() Callable[[Any], None][source]

Assert that a value is None.

Return type:

typing.Callable[[typing.Any], None]

assert_number() Callable[[Any], int | float][source]

Assert that a value is a number (a Python int or float).

Return type:

typing.Callable[[typing.Any], int | float]

assert_or(if_assertion: Callable[[betty.serde.load.ValueT], betty.serde.load.ReturnT], else_assertions: Callable[[betty.serde.load.ValueT], betty.serde.load.ReturnU]) Callable[[betty.serde.load.ValueT], betty.serde.load.ReturnT | betty.serde.load.ReturnU][source]

Assert that at least one of the given assertions passed.

Parameters:
Return type:

typing.Callable[[typing.TypeVar(ValueT)], typing.Union[typing.TypeVar(ReturnT), typing.TypeVar(ReturnU)]]

assert_path() Callable[[Any], pathlib.Path][source]

Assert that a value is a path to a file or directory on disk that may or may not exist.

Return type:

typing.Callable[[typing.Any], pathlib.Path]

assert_positive_number() Callable[[Any], int | float][source]

Assert that a vaue is a positive nu,ber.

Return type:

typing.Callable[[typing.Any], int | float]

assert_record(fields: betty.serde.load.Fields) Callable[[Any], MutableMapping[str, Any]][source]

Assert that a value is a record: a key-value mapping of arbitrary value types, with a known structure.

To validate a key-value mapping as a records, assertions for all possible keys MUST be provided. Any keys present in the value for which no field assertions are provided will cause the entire record assertion to fail.

Parameters:

fields (betty.serde.load.Fields)

Return type:

typing.Callable[[typing.Any], typing.MutableMapping[str, typing.Any]]

assert_sequence(item_assertion: betty.serde.load.Assertions[betty.serde.load.ValueT, betty.serde.load.ReturnT]) Callable[[Any], MutableSequence[betty.serde.load.ReturnT]][source]

Assert that a value is a sequence and that all item values are of the given type.

Parameters:

item_assertion (betty.serde.load.Assertions[typing.TypeVar(ValueT), typing.TypeVar(ReturnT)])

Return type:

typing.Callable[[typing.Any], typing.MutableSequence[typing.TypeVar(ReturnT)]]

assert_setattr(instance: object, attr_name: str) Callable[[betty.serde.load.ValueT], betty.serde.load.ValueT][source]

Set a value for the given object’s attribute.

Parameters:
Return type:

typing.Callable[[typing.TypeVar(ValueT)], typing.TypeVar(ValueT)]

assert_str() Callable[[Any], str][source]

Assert that a value is a Python str.

Return type:

typing.Callable[[typing.Any], str]

exception betty.serde.load.AssertionFailed[source]

Bases: LoadError

Raised when an assertion failed while deserializing data.

Parameters:

message (betty.locale.Localizable)

class betty.serde.load.Assertions[source]

Bases: _Assertions[CallValueT, CallReturnT, CallValueT], Generic[CallValueT, CallReturnT]

Start an assertions chain.

Assertion chains let you chain/link/combine assertions into pipelines that take an input value and, if the assertions pass, return an output value. Each chain may be (re)used as many times as needed.

Assertions chains are monads. While uncommon in Python, this allows us to create these chains in a type-safe way, and tools like mypy can confirm that all assertions in any given chain are compatible with each other.

Parameters:

_assertion (typing.Callable[[typing.TypeVar(FValueT)], typing.TypeVar(CallReturnT)])

class betty.serde.load.Fields[source]

Bases: object

A sequence of fields, used to assert key-value mappings.

Parameters:

fields (betty.serde.load._Field[typing.Any, typing.Any])

__init__(*fields: betty.serde.load._Field[Any, Any])[source]
Parameters:

fields (betty.serde.load._Field[typing.Any, typing.Any])

exception betty.serde.load.FormatError[source]

Bases: LoadError

Raised when data that is being deserialized is provided in an unknown (undeserializable) format.

Parameters:

message (betty.locale.Localizable)

exception betty.serde.load.LoadError[source]

Bases: SerdeError

Raised for any error while deserializing data.

Parameters:

message (betty.locale.Localizable)

class betty.serde.load.OptionalField[source]

Bases: Generic[ValueT, ReturnT], _Field[ValueT, ReturnT]

An optional key-value mapping field.

Parameters:
__init__(name: str, assertion: betty.serde.load._Assertions[betty.serde.load.ValueT, betty.serde.load.ReturnT, Any] | None = None) None
Parameters:
class betty.serde.load.RequiredField[source]

Bases: Generic[ValueT, ReturnT], _Field[ValueT, ReturnT]

A required key-value mapping field.

Parameters:
__init__(name: str, assertion: betty.serde.load._Assertions[betty.serde.load.ValueT, betty.serde.load.ReturnT, Any] | None = None) None
Parameters: