Skip to content
Sarj Code Standards

no-dual-shape-model-field-reader

Normalize a known model instead of reading its fields through a model-or-dict accessor.

Why

A string-selected object return discards the model's field types and duplicates the raw and validated representations at every read. Literal field names at all local callers prove this is a fixed record API.

Fix

Normalize raw input once with the named model's model_validate, then use typed attributes. Preserve missing-field, coercion, default and validation-error behavior; before validators still own raw input.

Examples

Before — flagged A fixed model field is hidden behind a string-selected object return
app/routes.py
from pydantic import BaseModel
class Route(BaseModel):
destination: str
def _field(route: object, name: str) -> object:
if isinstance(route, Route):
return getattr(route, name)
if isinstance(route, dict):
return route.get(name)
return None
def destination(raw: object) -> object:
return _field(raw, "destination")
After — preferred Normalize the input and read the typed field
app/routes.py
from pydantic import BaseModel
class Route(BaseModel):
destination: str
def destination(raw: object) -> str:
route = Route.model_validate(raw)
return route.destination