c/backend › command
0votes

Cursor rules for Python projects

TemplateLow risk Tested
submitted by CommandVault editors to c/backend · 3 uses

A .cursor/rules/python.mdc for typed, tested, well-structured Python.

Template · 3 variables
---
description: Python conventions
globs: ["**/*.py"]
alwaysApply: false
---

# Python

- Python {{python_version}}; type hints on every function signature; `mypy` clean.
- Format and lint with ruff (`ruff format`, `ruff check --fix`).
- Small pure functions; side effects (I/O, network, DB) at the edges.
- Use dataclasses or Pydantic models instead of loose dicts for structured data.
- Raise specific exceptions; never use bare `except:`.
- Tests with pytest in `tests/`, mirroring the package layout; use fixtures, not globals.
- Dependencies managed with {{dependency_tool}}; don't add packages without saying why.

## When you change code
- Follow the patterns in the nearest existing file before inventing new ones.
- Add or update a test for behavior changes; run `{{test_command}}`.
- Keep diffs small; explain anything surprising in your reply.
0 commentsreport

Guide

What you get: The agent writes typed, ruff-clean, mypy-clean Python with dataclasses instead of dicts, specific exceptions, I/O kept at the edges, and pytest tests in tests/ mirroring the package. Best for Python teams who want an agent to match their typing, linting and test conventions..

Before you start

  • A Python project with ruff, mypy and pytest installed as dev dependencies
  • Fill python_version, dependency_tool and test_command (we used 3.12, uv, uv run pytest -q)
  • Cursor: .cursor/rules/python.mdc. Claude Code/Codex: the same text in CLAUDE.md / AGENTS.md

Worked example (Nov 14, 2026, Claude Code 2.1.318 (claude-opus-5-5); Codex 0.151.0 (gpt-5.6-sol))

Input

A small uv package (invoicekit, src layout, mypy strict) with an existing parse_amount helper. Task: add an importer for supplier CSV invoices (sku, description, qty, unit_price, vat_rate) returning line items and net/VAT/gross totals, reporting malformed rows with row numbers.

Result

Claude Code: frozen dataclasses InvoiceLine/InvoiceTotals/RowError, read_invoice_csv(path) wrapping a pure parse_invoice_csv(text), InvalidRowError for bad rows; 15 tests pass. Codex: frozen slotted dataclasses, import_supplier_csv(path or stream), row-numbered InvoiceRowError list; 11 tests pass.

Not tested in Cursor or Copilot: the rule text was saved verbatim (front matter included) as CLAUDE.md for Claude Code and AGENTS.md for Codex, the files those agents load at startup, plus a copy at its normal path. Re-run by us: uv run pytest -q, uv run mypy src tests (strict), ruff check and ruff format --check all clean for both; no bare except; no new dependencies. Both reused parse_amount and both also fixed it to reject 'NaN'/'Infinity' (and said so).

How to check it worked

  • Run the four checks yourself: pytest, mypy, ruff check, ruff format --check
  • Check pyproject.toml/uv.lock for unexplained new dependencies

Common problems

  • mypy not configured (no [tool.mypy]) makes 'mypy clean' meaningless; set strict or your own flags
  • The agent may change existing helpers it reuses; check the diff for edits outside the new module

How to add this Cursor rule

  1. Fill in the 3 blanks above; Copy puts your answers into the file.
  2. Save it in .cursor/rules/ with the .mdc extension (plain .md files there are ignored), e.g. .cursor/rules/nextjs.mdc.
  3. The front matter decides when it applies: alwaysApply: true for every chat, globs for matching files, or a description so Cursor applies it when relevant.
  4. Check it in Cursor's Rules settings, then ask something about a matching file and confirm the answer follows the rule.
  • {{python_version}} Python version
  • {{dependency_tool}} Dependency tool
  • {{test_command}} Test command

Checked against Cursor docs: rules.

More from c/backend

See all of c/backend →
0 comments

Log in to join the discussion.