betty.assertion package

Submodules

Module contents

The Assertion API.

class betty.assertion.AssertionChain[source]

Bases: Generic[_AssertionValueT, _AssertionReturnT]

An assertion 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.

Assertion chains are assertions themselves: you can use a chain wherever you can use a ‘plain’ assertion.

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(_AssertionValueT)], typing.TypeVar(_AssertionReturnT)])

__init__(_assertion: Callable[[betty.assertion._AssertionValueT], betty.assertion._AssertionReturnT])[source]
Parameters:

_assertion (typing.Callable[[typing.TypeVar(_AssertionValueT)], typing.TypeVar(_AssertionReturnT)])

chain(assertion: Callable[[betty.assertion._AssertionReturnT], betty.assertion._AssertionsExtendReturnT]) betty.assertion.AssertionChain[betty.assertion._AssertionValueT, betty.assertion._AssertionsExtendReturnT][source]

Extend the chain with the given assertion.

Parameters:

assertion (typing.Callable[[typing.TypeVar(_AssertionReturnT)], typing.TypeVar(_AssertionsExtendReturnT)])

Return type:

betty.assertion.AssertionChain[typing.TypeVar(_AssertionValueT), typing.TypeVar(_AssertionsExtendReturnT)]

class betty.assertion.OptionalField[source]

Bases: Generic[_AssertionValueT, _AssertionReturnT], _Field[_AssertionValueT, _AssertionReturnT]

An optional key-value mapping field.

Parameters:
__init__(name: str, assertion: Callable[[betty.assertion._AssertionValueT], betty.assertion._AssertionReturnT] | None = None) None
Parameters:
class betty.assertion.RequiredField[source]

Bases: Generic[_AssertionValueT, _AssertionReturnT], _Field[_AssertionValueT, _AssertionReturnT]

A required key-value mapping field.

Parameters:
__init__(name: str, assertion: Callable[[betty.assertion._AssertionValueT], betty.assertion._AssertionReturnT] | None = None) None
Parameters:
betty.assertion.assert_bool() betty.assertion.AssertionChain[Any, bool][source]

Assert that a value is a Python bool.

Return type:

betty.assertion.AssertionChain[typing.Any, bool]

betty.assertion.assert_dict() betty.assertion.AssertionChain[Any, dict[str, Any]][source]

Assert that a value is a Python dict.

Return type:

betty.assertion.AssertionChain[typing.Any, dict[str, typing.Any]]

betty.assertion.assert_directory_path() betty.assertion.AssertionChain[Any, pathlib.Path][source]

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

Return type:

betty.assertion.AssertionChain[typing.Any, pathlib.Path]

betty.assertion.assert_field(field: betty.assertion._Field[betty.assertion._AssertionValueT, betty.assertion._AssertionReturnT]) betty.assertion.AssertionChain[betty.assertion._AssertionValueT, betty.assertion._AssertionReturnT] | betty.assertion.AssertionChain[betty.assertion._AssertionValueT, betty.assertion._AssertionReturnT | type[betty.typing.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.assertion._Field[typing.TypeVar(_AssertionValueT), typing.TypeVar(_AssertionReturnT)])

Return type:

typing.Union[betty.assertion.AssertionChain[typing.TypeVar(_AssertionValueT), typing.TypeVar(_AssertionReturnT)], betty.assertion.AssertionChain[typing.TypeVar(_AssertionValueT), typing.Union[typing.TypeVar(_AssertionReturnT), type[betty.typing.Void]]]]

betty.assertion.assert_fields(*fields: betty.assertion._Field[Any, Any]) betty.assertion.AssertionChain[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.assertion._Field[typing.Any, typing.Any])

Return type:

betty.assertion.AssertionChain[typing.Any, typing.MutableMapping[str, typing.Any]]

betty.assertion.assert_file_path() betty.assertion.AssertionChain[Any, pathlib.Path][source]

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

Return type:

betty.assertion.AssertionChain[typing.Any, pathlib.Path]

betty.assertion.assert_float() betty.assertion.AssertionChain[Any, float][source]

Assert that a value is a Python float.

Return type:

betty.assertion.AssertionChain[typing.Any, float]

betty.assertion.assert_int() betty.assertion.AssertionChain[Any, int][source]

Assert that a value is a Python int.

Return type:

betty.assertion.AssertionChain[typing.Any, int]

betty.assertion.assert_list() betty.assertion.AssertionChain[Any, list[Any]][source]

Assert that a value is a Python list.

Return type:

betty.assertion.AssertionChain[typing.Any, list[typing.Any]]

betty.assertion.assert_locale() betty.assertion.AssertionChain[Any, str][source]

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

Return type:

betty.assertion.AssertionChain[typing.Any, str]

betty.assertion.assert_mapping(item_assertion: Callable[[Any], betty.assertion._AssertionReturnT]) betty.assertion.AssertionChain[Any, MutableMapping[str, betty.assertion._AssertionReturnT]][source]

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

Parameters:

item_assertion (typing.Callable[[typing.Any], typing.TypeVar(_AssertionReturnT)])

Return type:

betty.assertion.AssertionChain[typing.Any, typing.MutableMapping[str, typing.TypeVar(_AssertionReturnT)]]

betty.assertion.assert_none() betty.assertion.AssertionChain[Any, None][source]

Assert that a value is None.

Return type:

betty.assertion.AssertionChain[typing.Any, None]

betty.assertion.assert_number() betty.assertion.AssertionChain[Any, int | float][source]

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

Return type:

betty.assertion.AssertionChain[typing.Any, int | float]

betty.assertion.assert_or(if_assertion: Callable[[betty.assertion._AssertionValueT], betty.assertion._AssertionReturnT], else_assertion: Callable[[betty.assertion._AssertionValueT], betty.assertion._AssertionReturnU]) betty.assertion.AssertionChain[betty.assertion._AssertionValueT, betty.assertion._AssertionReturnT | betty.assertion._AssertionReturnU][source]

Assert that at least one of the given assertions passed.

Parameters:
Return type:

betty.assertion.AssertionChain[typing.TypeVar(_AssertionValueT), typing.Union[typing.TypeVar(_AssertionReturnT), typing.TypeVar(_AssertionReturnU)]]

betty.assertion.assert_path() betty.assertion.AssertionChain[pathlib.Path | str, 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:

betty.assertion.AssertionChain[pathlib.Path | str, pathlib.Path]

betty.assertion.assert_positive_number() betty.assertion.AssertionChain[Any, int | float][source]

Assert that a vaue is a positive nu,ber.

Return type:

betty.assertion.AssertionChain[typing.Any, int | float]

betty.assertion.assert_record(*fields: betty.assertion._Field[Any, Any]) betty.assertion.AssertionChain[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.assertion._Field[typing.Any, typing.Any])

Return type:

betty.assertion.AssertionChain[typing.Any, typing.MutableMapping[str, typing.Any]]

betty.assertion.assert_sequence(item_assertion: Callable[[Any], betty.assertion._AssertionReturnT]) betty.assertion.AssertionChain[Any, MutableSequence[betty.assertion._AssertionReturnT]][source]

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

Parameters:

item_assertion (typing.Callable[[typing.Any], typing.TypeVar(_AssertionReturnT)])

Return type:

betty.assertion.AssertionChain[typing.Any, typing.MutableSequence[typing.TypeVar(_AssertionReturnT)]]

betty.assertion.assert_setattr(instance: object, attr_name: str) betty.assertion.AssertionChain[Any, Any][source]

Set a value for the given object’s attribute.

Parameters:
Return type:

betty.assertion.AssertionChain[typing.Any, typing.Any]

betty.assertion.assert_str() betty.assertion.AssertionChain[Any, str][source]

Assert that a value is a Python str.

Return type:

betty.assertion.AssertionChain[typing.Any, str]