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:
  • model (BaseModel) – The model to check.

  • fields (Iterable[str]) – The attributes that should not be set.

  • loc (Sequence[str | int]) – The location prefix for the reported diagnosis.

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:
  • model (BaseModel) – The model to check.

  • fields (Iterable[str]) – The attributes that are mutually exclusive.

  • loc (Sequence[str | int]) – The location prefix for the reported diagnosis.

  • require_one (bool) – Whether exactly one field is required to be set.

Yields:
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:
  • model (BaseModel) – The model to check.

  • fields (Iterable[str]) – The attributes that are required.

  • loc (Sequence[str | int]) – The location prefix for the reported diagnosis.

  • accept_empty (bool) – Whether empty values (e.g., empty strings, empty lists) are accepted as valid input.

Yields: