Skip to content

chore: add drf-lint pre-commit hook with baseline#3861

Open
blarghmatey wants to merge 1 commit intomasterfrom
chore/add-drf-lint-pre-commit
Open

chore: add drf-lint pre-commit hook with baseline#3861
blarghmatey wants to merge 1 commit intomasterfrom
chore/add-drf-lint-pre-commit

Conversation

@blarghmatey
Copy link
Copy Markdown
Member

What are the relevant tickets?

N/A

Description (What does it do?)

Adds the drf-serializer-orm-check pre-commit hook from mitodl/ol-django which uses mitol-drf-lint to detect Django ORM queries inside DRF serializer methods — a common source of N+1 query bugs.

Also generates an initial drf_lint_baseline.json recording all pre-existing violations so the hook only flags new issues going forward. Existing violations can be addressed incrementally.

mitol-drf-lint is added as a dev dependency to support running the tool locally outside of pre-commit.

How can this be tested?

  1. Install pre-commit hooks: pre-commit install
  2. Modify or create a serializers.py file that calls ORM methods (e.g. .filter(), .get()) inside a serializer method body
  3. Attempt to commit — the hook should flag the new violation
  4. Alternatively, run directly: uv run drf-lint <path/to/serializers.py>

Additional Context

The baseline was generated with:

uv run drf-lint --generate-baseline --baseline drf_lint_baseline.json $(find . -name "serializers.py" ! -path "*/migrations/*")

To regenerate after fixing existing violations, rerun the above command.

@blarghmatey blarghmatey force-pushed the chore/add-drf-lint-pre-commit branch 4 times, most recently from 0db4e0c to 90a749b Compare April 1, 2026 20:39
@blarghmatey blarghmatey force-pushed the chore/add-drf-lint-pre-commit branch from 90a749b to 45fabe2 Compare April 1, 2026 20:50
Adds the drf-serializer-orm-check hook (mirroring the definition from
mitodl/ol-django) to detect Django ORM queries inside DRF serializer
methods (N+1 risk). Uses repo: local so pre-commit only installs
mitol-drf-lint without attempting to pip-install the ol-django workspace
root (which has internal workspace dependencies unavailable on PyPI).

Generates an initial baseline file (drf_lint_baseline.json) recording
existing violations so the hook only flags new issues going forward.

Co-authored-by: Copilot <[email protected]>
@blarghmatey blarghmatey force-pushed the chore/add-drf-lint-pre-commit branch from 45fabe2 to f72c2e2 Compare April 2, 2026 19:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant