PY-MODE0004 · manual_model_attribute_projection_count
Count structures that manually repeat fields from one model instance.
This is a deterministic rule for python. Read its implementation.
Definition
Section titled “Definition”Inspect dictionary literals, sequences of key and value pairs, and keyword calls. Group direct
attribute reads by their root object when each output key matches the attribute name after
hyphen normalization. Report a structure that repeats at least minimum_attributes distinct
fields. This is evidence that a Pydantic model, dataclass, or similar typed value already owns
the schema and should provide the projection.
Evidence
Section titled “Evidence”Each finding records the source range, root object, distinct projected attribute count, and every repeated attribute. The rule does not require static proof of the root type because the matching keys and configured count form the conservative structural signal. The value is the number of structures repeating enough fields of one model instance.
Exceptions
Section titled “Exceptions”Different output names, computed values involving several attributes, unpacking, positional
constructor calls, and projections below the threshold are ignored. Explicit projection can
remain when the target schema intentionally differs, excludes secrets, or requires a stable
compatibility boundary. Prefer model_dump include or exclude controls, a typed conversion
model, or one named serializer over a second handwritten field list.
Examples
Section titled “Examples”Bad
A tuple manually lists `("id", definition.id)`, `("summary", definition.summary)`, and manymore matching fields before rendering them.
Gooddefinition.model_dump(mode="json", exclude_defaults=True) reads the model-owned schema.
A four-field API payload whose names and transformations deliberately differ remains explicit.
References
Section titled “References”- Cites “Pydantic documentation”, model serialization. Open reference
- Cites “The Python Standard Library”, dataclasses
asdict. Open reference - Cites “Refactoring”, Data Class and Extract Class