tugboat.constraints module¶
This module provides some generic constraints that can be used on linting models.
All functions in this module are generators that yield Diagnosis
objects when a constraint is not met. These functions can be used in analysis
hooks, yielding results using the yield from syntax.
A typical usage of these functions is as follows:
from tugboat import hookimpl
from tugboat.constraints import mutually_exclusive
@hookimpl
def analyze_workflow(workflow: Workflow) -> Iterator[Diagnosis]:
yield from mutually_exclusive(
workflow.metadata,
fields=["name", "generateName"],
loc=("metadata",),
require_one=True,
)
Tip
The loc parameter provides a convenient way to specify the location
context without needing to wrap constraint functions with prepend_loc().
- tugboat.constraints.accept_none(model: BaseModel, *, fields: Iterable[str], loc: Sequence[str | int] = ()) Iterator[Diagnosis]¶
Check if all the specified fields are not set.
- Parameters:
- Yields:
M102 Found redundant field for each unexpected field.
- tugboat.constraints.mutually_exclusive(model: BaseModel, *, fields: Iterable[str], loc: Sequence[str | int] = (), require_one: bool = False) Iterator[Diagnosis]¶
Ensures that at most one of the specified fields in the model is set, but does not require any of them to be set.
- Parameters:
- Yields:
M101 Missing required field when none of the fields are set, if
require_oneisTrue.M201 Mutually exclusive fields when more than one were set.
- tugboat.constraints.require_all(model: BaseModel, *, fields: Iterable[str], loc: Sequence[str | int] = (), accept_empty: bool = False) Iterator[Diagnosis]¶
Requires that all of the specified fields in the model are set.
- Parameters:
- Yields:
M101 Missing required field when any of the fields are absent.
M202 Empty input when any of the fields are empty, only if
accept_emptyisFalse.