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.

__init__(_assertion: Callable[[_AssertionValueT], _AssertionReturnT])[source]
chain(assertion: Callable[[_AssertionReturnT], _AssertionsExtendReturnT]) AssertionChain[_AssertionValueT, _AssertionsExtendReturnT][source]

Extend the chain with the given assertion.

class betty.assertion.OptionalField[source]

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

An optional key-value mapping field.

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

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

A required key-value mapping field.

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

Assert that a value is a Python bool.

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

Assert that a value is a Python dict.

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

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

betty.assertion.assert_field(field: RequiredField[_AssertionValueT, _AssertionReturnT]) AssertionChain[_AssertionValueT, _AssertionReturnT][source]
betty.assertion.assert_field(field: OptionalField[_AssertionValueT, _AssertionReturnT]) AssertionChain[_AssertionValueT, _AssertionReturnT | type[Void]]

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

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

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

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

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

Assert that a value is a Python float.

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

Assert that a value is a Python int.

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

Assert that a value is a Python list.

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

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

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

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

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

Assert that a value is None.

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

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

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

Assert that at least one of the given assertions passed.

betty.assertion.assert_path() AssertionChain[Path | str, Path][source]

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

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

Assert that a vaue is a positive nu,ber.

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

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

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

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

Set a value for the given object’s attribute.

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

Assert that a value is a Python str.