Expression¶
expression
¶
Expression-backed specification implementation.
Wraps a callable predicate. Inherits composition from ComposableSpecification.
ExpressionSpecification
¶
Bases: ComposableSpecification[T]
Specification defined by a callable predicate.
Wraps a user-provided function that returns True/False.
Composition operators are inherited from ComposableSpecification—
do NOT reimplement them here.
Example
is_active = ExpressionSpecification(
... lambda user: user.is_active, description="is_active"
... )
is_admin = ExpressionSpecification(
... lambda user: user.role == "admin", description="is_admin"
... )
active_admin = is_active & is_admin # Uses inherited operator
Source code in src/forging_blocks/foundation/specification/expression.py
__init__(predicate: Callable[[T], bool], description: str = '') -> None
¶
Initialize the expression specification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicate
|
Callable[[T], bool]
|
A callable that accepts a candidate and returns bool. |
required |
description
|
str
|
Optional human-readable label for repr/debugging. |
''
|
Raises:
| Type | Description |
|---|---|
NotCallablePredicateError
|
If predicate is not callable. |
Source code in src/forging_blocks/foundation/specification/expression.py
is_satisfied_by(candidate: T) -> bool
¶
Evaluate the predicate against the candidate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
candidate
|
T
|
The object to test. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
The result of calling self._predicate(candidate). |
Source code in src/forging_blocks/foundation/specification/expression.py
__repr__() -> str
¶
Return a string representation for debugging.
Uses the description if provided, otherwise extracts the function name.