Guards¶
Register and evaluate guards used in transitions.
GuardRegistry
¶
Registry for guard functions (sync and async).
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.
Source code in src/pystator/guards.py
unregister
¶
Unregister a guard function. Thread-safe.
Source code in src/pystator/guards.py
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
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
GuardEvaluator
¶
Evaluates guard conditions for transitions.
Supports named guards (from registry) and inline expressions.
Source code in src/pystator/guards.py
can_transition
¶
Check if a transition is allowed based on its guards.
Source code in src/pystator/guards.py
get_required_guards
¶
Return the set of named guard names required by the given transitions.
Excludes inline expression guards (only returns guard names from registry).
Source code in src/pystator/guards.py
evaluate_and_raise
¶
Evaluate guards and raise GuardRejectedError if blocked.
Source code in src/pystator/guards.py
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.
negate
¶
Guard that negates the result of another guard.