Skip to content
Sarj Code Standards

no-pydantic-validation-opt-out

Report Pydantic strict-validation opt-outs.

Why

Lax field, schema, or call settings can coerce a value even when the surrounding Pydantic model uses strict validation.

Fix

Remove explicit lax overrides to keep the enclosing validation policy. Set strict=True in the validate_call or TypeAdapter configuration, or at the adapter validation call.

Examples

Before — flagged A field can opt out of strict model validation
app/models.py
from pydantic import BaseModel, Field
class RetryPolicy(BaseModel, strict=True):
attempts: int = Field(strict=False)
After — preferred The field inherits the model's strict validation
app/models.py
from pydantic import BaseModel
class RetryPolicy(BaseModel, strict=True):
attempts: int
Before — flagged An adapter uses lax validation by default
app/models.py
from pydantic import TypeAdapter
def parse_attempt(value: str | int) -> int:
return TypeAdapter(int).validate_python(value)
After — preferred An adapter can require strict validation at the call
app/models.py
from pydantic import TypeAdapter
def parse_attempt(value: str | int) -> int:
return TypeAdapter(int).validate_python(value, strict=True)