Hide navigation sidebar
Hide table of contents sidebar
Skip to content
Back to top
View this page
Toggle table of contents sidebar

# betty.assertion package

## Submodules

- betty.assertion.error module
  - `AssertionFailed`
    - `AssertionFailed.__init__()`
    - `AssertionFailed.contexts`
    - `AssertionFailed.localize()`
    - `AssertionFailed.raised()`
    - `AssertionFailed.with_context()`
  - `AssertionFailedGroup`
    - `AssertionFailedGroup.__init__()`
    - `AssertionFailedGroup.append()`
    - `AssertionFailedGroup.assert_valid()`
    - `AssertionFailedGroup.catch()`
    - `AssertionFailedGroup.invalid`
    - `AssertionFailedGroup.localize()`
    - `AssertionFailedGroup.raised()`
    - `AssertionFailedGroup.valid`
    - `AssertionFailedGroup.with_context()`

## Module contents

The Assertion API.

class betty.assertion.AssertionChain

  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])
  chain(assertion: Callable[[_AssertionReturnT], _AssertionsExtendReturnT]) → AssertionChain[_AssertionValueT, _AssertionsExtendReturnT]

    Extend the chain with the given assertion.

class betty.assertion.OptionalField

  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

  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]

  Assert that a value is a Python `bool`.

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

  Assert that a value is a Python `dict`.

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

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

betty.assertion.assert_field(field: RequiredField[_AssertionValueT, _AssertionReturnT]) → AssertionChain[_AssertionValueT, _AssertionReturnT]
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]]

  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]

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

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

  Assert that a value is a Python `float`.

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

  Assert that a value is a Python `int`.

betty.assertion.assert_isinstance(alleged_type: type[_AssertionValueT]) → Callable[[Any], _AssertionValueT]

  Assert that a value is an instance of the given type.

  This assertion is NOT optimized to be user-facing (it is untranslated) because Python types are not user-facing.

betty.assertion.assert_len_min(minimum: int = 0) → AssertionChain[Sized, Sized]

  Assert that a value has a minimum length.

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

  Assert that a value is a Python `list`.

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

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

betty.assertion.assert_mapping(item_assertion: Callable[[Any], _AssertionReturnT], key_assertion: Callable[[Any], _AssertionKeyT] | None = None) → AssertionChain[Any, MutableMapping[str, _AssertionReturnT]]

  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]

  Assert that a value is `None`.

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

  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]

  Assert that at least one of the given assertions passed.

betty.assertion.assert_path() → AssertionChain[Any, Path]

  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]

  Assert that a vaue is a positive nu,ber.

betty.assertion.assert_record(*fields: _Field[Any, Any]) → AssertionChain[Any, MutableMapping[str, Any]]

  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]]

  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]

  Set a value for the given object’s attribute.

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

  Assert that a value is a Python `str`.