Skip to content

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, 1
    affirm(0) # raises AssertionError
  • Callable: a function that takes the value and returns a boolean. If a lambda function is used, its source code will be included in the error message.
    affirm(5, lambda x: x > 0) # OK, 5
    affirm(-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, False
    affirm(True, False) # raises AssertionError

API#

Attributes

NameDescription
UNo description.

Classes

NameDescription
AffirmPredicateAn abstract base class for complex predicates within the affirm function.
is_typeAssert whether the value is exactly the specified type.

Functions

NameDescription
affirmAssert the value according to the predicate, return it if it is true.

Uattributemodule attribute#

U = TypeVar('U', covariant=False, contravariant=False)

AffirmPredicateclass#

class AffirmPredicate(ABCDispatch)

Bases: ABCDispatch

An abstract base class for complex predicates within the affirm function.

__call__method#

def __call__(value: T) -> bool

Test if the value satisfies the predicate. This function enforces returning a boolean value.

Parameters

NameTypeDescription
valueTThe value to be tested.

Returns

bool
Whether the value satisfies the predicate.

fail_messagemethodabstract#

def fail_message(argument_expr: ast.expr | None, value: T) -> str

When the predicate fails, convert inputs to a formattable string explaining the reason.

Parameters

NameTypeDescription
argument_exprast.expr | NoneThe AST expression of the argument passed to affirm, before evaluation. May be None if not available.
valueTThe actual value being affirmed.

Returns

str
A string explaining the reason for failure.

testmethodabstract#

def test(value: T) -> bool

Test if the value satisfies the predicate.

Parameters

NameTypeDescription
valueTThe 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.

tyattributeclass attributeinstance attribute#

ty = object

__call__method#

def __call__(value) -> bool

__class_getitem__method#

def __class_getitem__(item)

fail_messagemethod#

def fail_message(argument_expr: ast.expr | None, value) -> str

testmethod#

def test(value) -> bool

typemethodcachedclassmethod#

def type(ty)

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

Assert 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

NameTypeDefaultDescription
valueTrequiredThe value to be asserted.
predicateCallable[[T], bool] | bool | NoneNoneThe predicate to be used for assertion. If None, evaluate the truthiness of the value.

Returns

TU | T
The value asserted.

Raises

AssertionError
If the value is not true.