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 tugboat.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 require_exactly_one
@hookimpl
def analyze_workflow(workflow: Workflow) -> Iterator[Diagnosis]:
yield from require_exactly_one(
model=workflow.metadata,
loc=("metadata",),
fields=["name", "generateName"],
)
- tugboat.constraints.accept_none(*, model: BaseModel, loc: Sequence[str | int], fields: Iterable[str]) Iterator[Diagnosis]¶
Check if all the specified fields are not set.
- Yields:
M102 Found redundant field for each unexpected field.
- tugboat.constraints.mutually_exclusive(*, model: Any, loc: Sequence[str | int], fields: Iterable[str]) 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.
- Yields:
M201 Mutually exclusive fields when more than one were set.
- tugboat.constraints.require_all(*, model: Any, loc: Sequence[str | int], fields: Iterable[str]) Iterator[Diagnosis]¶
Requires that all of the specified fields in the model are set.
- Yields:
M101 Missing required field when any of the fields are absent.
- tugboat.constraints.require_exactly_one(*, model: Any, loc: Sequence[str | int], fields: Iterable[str]) Iterator[Diagnosis]¶
Requires that exactly one of the specified fields in the model is set.
- Yields:
M101 Missing required field when none of the fields are set.
M201 Mutually exclusive fields when more than one were set.
- tugboat.constraints.require_non_empty(*, model: Any, loc: Sequence[str | int], fields: Iterable[str]) Iterator[Diagnosis]¶
Requires that all of the specified fields are set and not empty.
- Yields:
M101 Missing required field when any of the fields are absent.
M202 Empty input when any of the fields are empty.