Skip to content

Python · correctness

prefer-class-row

python:prefer-class-row

Use a validated model row instead of Psycopg `dict_row`.

Code
SARJ013
Default
error
Fix
none
Languages
python

Why

Dictionary rows cross the database boundary without validating field names or values against a model.

Fix

Pass `class_row(Model)` as the row factory for queries that return a stable model shape.

Before / after

Executed by this rule’s unit tests.

Before

Cursor returns unvalidated dictionaries

app/task_store.py · focus
from psycopg.rows import dict_row

cursor = connection.cursor(row_factory=dict_row)

After

Cursor validates rows into a model

app/task_store.py · focus
from psycopg.rows import class_row

cursor = connection.cursor(row_factory=class_row(Task))

Limits

  • The rule matches any `row_factory` keyword whose value ends in `dict_row`.
  • Ad hoc or dynamically selected row shapes require a local suppression when a class row is unsuitable.