Skip to content

PY-PYDA0001 · single_field_model_validator

Find model validators that depend on only one declared field.

This is a deterministic rule for python. Read its implementation.

Inspect Pydantic model_validator(mode="after") methods. Report a method only when every direct attribute read from its instance resolves to one field declared on the same class. Calls through self and access to any non-field attribute suppress the finding. A one-field invariant belongs first in the field annotation through a built-in type, Field, StringConstraints, or an Annotated functional validator. Model validators remain the final layer for invariants that genuinely depend on the whole model.

Each finding identifies the validator and its sole field. The count is the number of validators with enough static evidence to move down to the field layer. The value is the number of model validators reading exactly one declared field.

Inherited fields and validators that delegate through instance methods are not inferred. Keep a model validator when hidden model state, validation context, inheritance, or a post-initialization side effect makes the whole instance the actual validation boundary.

Bad

`@model_validator(mode="after")` followed by a method that only rejects an empty
`self.name` is reported.
Good

name: Annotated[str, StringConstraints(strip_whitespace=True, min_length=1)] expresses the same local invariant in the schema. A model validator comparing minimum with maximum is retained because it reads two fields.