📁 Agents
Cursor Rules for Python: .mdc Rule Set From Your Repo Facts
Generate Cursor project rules for a Python repo: .mdc files with globs for source, tests, and migrations, built from your real tooling, layout, and commands.
0Reviews
Prompt
Act as a Python tech lead who writes Cursor project rules for a team repo: short .mdc files in .cursor/rules/ that tell Cursor's Agent how this codebase is built, tested, and typed, using only facts from the repo. Inputs: - Python version, package manager, formatter, linter, and type checker as configured: [PythonToolchain] - Top-level folders and what lives in each: [RepoLayout] - Framework and the patterns the code follows (for example FastAPI routers, Django apps, plain scripts): [FrameworkPatterns] - Test runner, test folder, fixtures, and how external services are faked: [TestSetup] - Commands that exist in this repo, copied from the Makefile, pyproject scripts, or CI: [RealCommands] - Things the Agent must never do here: [NeverDo] - Mistakes the Agent has already made in this repo: [AgentMistakes] Generate: 1. A file plan: each .mdc file name, its rule type (Always Apply, Apply to Specific Files, Apply Intelligently, or Apply Manually), and why. 2. python-core.mdc: frontmatter with globs for source files and alwaysApply false, then rules for typing, imports, error handling, logging, and style that match PythonToolchain (point to the formatter instead of restating style). 3. tests.mdc: globs for the test folder only; how to write and name tests, use fixtures, and fake services per TestSetup. 4. One more file for the riskiest area in RepoLayout (for example migrations or the API layer), with a description so the Agent can pull it in when relevant. 5. A short always-apply file with the RealCommands and the NeverDo list. 6. A check list: every command, path, and tool named in the rules, each traced to an input, so nothing was invented. 7. Two test prompts to paste into Agent to confirm the rules load and change its behavior. Rules: - Name only tools, paths, and commands that appear in the inputs. - Keep each file under 60 lines; the whole set well under 500 lines. - Each line in AgentMistakes becomes one direct rule.
Instructions
Fill in the brackets with facts from your own repo (pyproject.toml, folder layout, the commands you really run) and paste it into Cursor's Agent chat, or into ChatGPT or Claude. Save each file it returns under .cursor/rules/ with the .mdc extension; a plain .md file in that folder is ignored. The single .cursorrules file in the repo root is the legacy format, so prefer .mdc project rules. Commit the rules, then add a line only when the Agent repeats the same mistake.
Generated Output
This image was generated using the prompt above.

Examples
Example Input
PythonToolchain: Python 3.12, uv for dependencies, ruff for lint and format (line length 100), mypy in strict mode on app/ only. RepoLayout: app/ (FastAPI service: routers/, services/, models/), migrations/ (Alembic), tests/ (pytest), scripts/ (one-off data jobs). FrameworkPatterns: routers stay thin and call a service function; Pydantic v2 models for every request and response; SQLAlchemy 2.0 sessions passed in through a dependency. TestSetup: pytest with fixtures in tests/conftest.py; the database is a throwaway Postgres container started by the fixture; HTTP calls to the payments API are faked with respx. RealCommands: uv run pytest, uv run ruff check --fix, uv run ruff format, uv run mypy app, uv run alembic revision --autogenerate -m "...". NeverDo: edit an existing migration file; add a dependency without asking; call the real payments API. AgentMistakes: used pip install in a suggestion; put SQL queries directly in routers; wrote tests that hit the network.
Example Output
1. FILE PLAN - .cursor/rules/project.mdc: Always Apply. Commands and never-do list. - .cursor/rules/python-core.mdc: Apply to Specific Files, globs app/**/*.py, scripts/**/*.py. - .cursor/rules/tests.mdc: Apply to Specific Files, globs tests/**/*.py. - .cursor/rules/migrations.mdc: Apply Intelligently (description), for Alembic work. 2. python-core.mdc --- globs: app/**/*.py, scripts/**/*.py alwaysApply: false --- - Target Python 3.12. Type every function signature; mypy runs in strict mode on app/. - Formatting and lint come from ruff (line length 100). Run uv run ruff format and uv run ruff check --fix instead of hand-formatting. - Routers in app/routers/ stay thin: validate with a Pydantic v2 model, call a function in app/services/, return a response model. - Never write SQL or session queries in a router. Data access lives in app/services/ and takes the SQLAlchemy 2.0 session from the dependency. - Raise specific exceptions in services; turn them into HTTP errors only in routers. 3. tests.mdc --- globs: tests/**/*.py alwaysApply: false --- - Use pytest and the fixtures in tests/conftest.py; add new shared fixtures there. - The database comes from the Postgres container fixture. Never point tests at another database. - Fake every payments API call with respx. A test must never make a real network call. - Name tests test_<behavior>_<condition>. 4. migrations.mdc --- description: Rules for creating Alembic migrations in migrations/ alwaysApply: false --- - Create a new revision with uv run alembic revision --autogenerate -m "short message", then read and fix the generated file. - Never edit an existing migration file. Add a new revision instead. 5. project.mdc --- alwaysApply: true --- - Dependencies are managed with uv. Never suggest pip install. Ask before adding any dependency. - Commands: uv run pytest, uv run ruff check --fix, uv run ruff format, uv run mypy app. - Never call the real payments API. 6. CHECK LIST - Python 3.12, uv, ruff 100, mypy strict app/: PythonToolchain. - app/routers, app/services, migrations/, tests/, scripts/: RepoLayout. - Pydantic v2, SQLAlchemy 2.0 dependency session: FrameworkPatterns. - conftest.py, Postgres container, respx: TestSetup. - All five commands: RealCommands. Never-do lines: NeverDo. - "Never suggest pip install", "Never write SQL in a router", "never make a real network call": AgentMistakes. 7. TEST PROMPTS - Open app/routers/orders.py and ask: "Add an endpoint that lists a customer's unpaid orders." Expect a service function and no SQL in the router. - Ask: "How do I add the httpx package?" Expect uv and a question before adding it.