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
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
¶
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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
GuardRegistry | None
|
Optional guard registry for named guard lookup. |
None
|
strict
|
bool
|
If True, raise on missing guards instead of passing. |
True
|
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.
Recurses into composite guards (allOf, anyOf, not). Excludes inline expression guards (only returns guard names from registry).
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.
Source code in src/pystator/guards.py
negate
¶
Guard that negates the result of another guard.