Guards¶
Register and evaluate guards used in transitions.
GuardRegistry
¶
Registry and evaluator for guard functions (sync and async).
Handles both registration of guard callables and evaluation of guard specs on transitions (named, expression, check, composite).
Example
registry = GuardRegistry() registry.register("is_valid", lambda ctx: ctx.get("valid", False)) registry.evaluate("is_valid", {"valid": True}) True
Source code in src/pystator/guards.py
register
¶
Register a guard function. Thread-safe.
Re-registration replaces the existing guard (allows overriding builtins).
If the function accepts keyword arguments beyond ctx, parameters
from the guard spec are passed as **kwargs instead of injected
into the context dict.
Source code in src/pystator/guards.py
unregister
¶
Unregister a guard function. Thread-safe.
Source code in src/pystator/guards.py
get
¶
Look up a registered guard callable by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The registered guard name. |
required |
Returns:
| Type | Description |
|---|---|
AnyGuardFunc
|
The guard callable. |
Raises:
| Type | Description |
|---|---|
GuardNotFoundError
|
If no guard is registered under name. |
Source code in src/pystator/guards.py
has
¶
is_async
¶
evaluate
¶
evaluate_all
¶
evaluate_all(guards: tuple[str, ...] | list[str], context: dict[str, Any], fail_fast: bool = True) -> GuardResult
Evaluate multiple guards. Returns GuardResult.
Source code in src/pystator/guards.py
async_evaluate
async
¶
Evaluate a single guard asynchronously.
Falls back to synchronous execution if the guard is not async.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Registered guard name. |
required |
context
|
dict[str, Any]
|
Context dict passed to the guard callable. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the guard passed, False otherwise. |
Source code in src/pystator/guards.py
async_evaluate_all
async
¶
async_evaluate_all(guards: tuple[GuardSpec, ...] | tuple[str, ...] | list[str], context: dict[str, Any], fail_fast: bool = True) -> GuardResult
Evaluate multiple guards asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
guards
|
tuple[GuardSpec, ...] | tuple[str, ...] | list[str]
|
Guard specs or names to evaluate. |
required |
context
|
dict[str, Any]
|
Context dict passed to each guard. |
required |
fail_fast
|
bool
|
If True, stop on first failing guard. |
True
|
Returns:
| Type | Description |
|---|---|
GuardResult
|
GuardResult summarising all evaluations. |
Source code in src/pystator/guards.py
list_guards
¶
clear
¶
can_transition
¶
can_transition(transition: Transition, context: dict[str, Any]) -> GuardResult
Check if a transition is allowed based on its guards.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transition
|
Transition
|
The transition whose guards to evaluate. |
required |
context
|
dict[str, Any]
|
Context dict passed to each guard. |
required |
Returns:
| Type | Description |
|---|---|
GuardResult
|
GuardResult indicating pass/fail with evaluation details. |
Source code in src/pystator/guards.py
async_can_transition
async
¶
async_can_transition(transition: Transition, context: dict[str, Any]) -> GuardResult
Async version of :meth:can_transition.
Falls back to sync evaluation for non-async guards.
Source code in src/pystator/guards.py
evaluate_and_raise
¶
Evaluate guards and raise GuardRejectedError if blocked.
Source code in src/pystator/guards.py
get_required_guards
¶
Return named guard names required by the given transitions.
Recurses into composite guards (allOf, anyOf, not). Excludes inline expression guards.
Source code in src/pystator/guards.py
decorator
¶
Decorator to register a guard function.
Example
@registry.decorator() ... def is_valid(ctx: dict) -> bool: ... return ctx.get("valid", False)
Source code in src/pystator/guards.py
GuardResult
dataclass
¶
GuardResult(passed: bool, guard_name: str | None = None, message: str = '', evaluated_guards: list[tuple[str, bool]] = list())
Result of guard evaluation.
Attributes:
| Name | Type | Description |
|---|---|---|
passed |
bool
|
Whether all guards passed. |
guard_name |
str | None
|
Name of the last evaluated guard (if failed). |
message |
str
|
Explanation of the result. |
evaluated_guards |
list[tuple[str, bool]]
|
List of (guard_name, passed) tuples. |
success
classmethod
¶
success(evaluated: list[tuple[str, bool]] | None = None) -> GuardResult
Create a passing guard result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
evaluated
|
list[tuple[str, bool]] | None
|
Optional list of (guard_name, passed) tuples. |
None
|
Returns:
| Type | Description |
|---|---|
GuardResult
|
GuardResult with |
Source code in src/pystator/guards.py
failure
classmethod
¶
failure(guard_name: str, message: str = '', evaluated: list[tuple[str, bool]] | None = None) -> GuardResult
Create a failing guard result with reason.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
guard_name
|
str
|
Name of the guard that failed. |
required |
message
|
str
|
Human-readable failure reason. |
''
|
evaluated
|
list[tuple[str, bool]] | None
|
Optional list of (guard_name, passed) tuples. |
None
|
Returns:
| Type | Description |
|---|---|
GuardResult
|
GuardResult with |
Source code in src/pystator/guards.py
Transition-level evaluation (can_transition, async_can_transition, …) lives on GuardRegistry (formerly a separate GuardEvaluator).
Built-in guard helpers:
equals
¶
greater_than
¶
Guard factory: context[key] > value.
Source code in src/pystator/guards.py
in_list
¶
Guard factory: context[key] in values.
all_of
¶
Compound guard: all must pass.
any_of
¶
Compound guard: at least one must pass.
Source code in src/pystator/guards.py
negate
¶
Guard that negates the result of another guard.