apfel.expr.affirm
The expression form of assert,
and related helper functions.
Also see assert!.
Usage
affirm allows asserting a value according to a predicate,
returning the value if the assertion is satisfied,
or raising an AssertionError with detailed information if not.
Similar to the builtin assert,
the affirm function will be a no-op when Python is run with optimizations (the -O flag, or setting __debug__ to False).
Some available forms of predicates:
- Unspecified or
None: evaluate the truthiness of the value.affirm(1) # OK, 1affirm(0) # raises AssertionError - Callable: a function that takes the value and returns a boolean.
If a
lambdafunction is used, its source code will be included in the error message.affirm(5, lambda x: x > 0) # OK, 5affirm(-3, lambda x: x > 0) # raises AssertionError - An
AffirmPredicate: a more complex predicate that can customize the failure message.affirm(True, type[int]) # OK, True (bool is a subclass of int)affirm(True, type[str]) # raises AssertionError - Boolean: an expression that is directly evaluated to a boolean.
Any value that can be converted to boolean is accepted.
affirm(False, True) # OK, Falseaffirm(True, False) # raises AssertionError
API#
Attributes
| Name | Description |
|---|---|
U | No description. |
Classes
| Name | Description |
|---|---|
AffirmPredicate | An abstract base class for complex predicates within the affirm function. |
is_type | Assert whether the value is exactly the specified type. |
Functions
| Name | Description |
|---|---|
affirm | Assert the value according to the predicate, return it if it is true. |
AffirmPredicateclass#
class AffirmPredicate(ABCDispatch)Bases: ABCDispatch
An abstract base class for complex predicates within the affirm function.
__call__method#
def __call__(value: T) -> boolTest if the value satisfies the predicate. This function enforces returning a boolean value.
Parameters
| Name | Type | Description |
|---|---|---|
value | T | The value to be tested. |
Returns
bool- Whether the value satisfies the predicate.
fail_messagemethodabstract#
def fail_message(argument_expr: ast.expr | None, value: T) -> strWhen the predicate fails, convert inputs to a formattable string explaining the reason.
Parameters
| Name | Type | Description |
|---|---|---|
argument_expr | ast.expr | None | The AST expression of the argument passed to affirm, before evaluation. May be None if not available. |
value | T | The actual value being affirmed. |
Returns
str- A string explaining the reason for failure.
testmethodabstract#
def test(value: T) -> boolTest if the value satisfies the predicate.
Parameters
| Name | Type | Description |
|---|---|---|
value | T | The value to be tested. |
Returns
bool- Whether the value satisfies the predicate.
is_typeclass#
class is_type(AffirmPredicate)Bases: AffirmPredicate
Assert whether the value is exactly the specified type.
affirmfunction#
def affirm(value: Any | T, predicate: type[is_type[U]] | type[type[T]] | AffirmPredicate[T] | Callable[[T], bool] | Callable[[T], Any] | bool | Any = None) -> U | TAssert the value according to the predicate, return it if it is true.
This is the expression form of assert.
See module-level documentation for detailed usage.
Parameters
Returns
TU | T- The value asserted.
Raises
AssertionError- If the value is not true.
