Cursor Rules: Python — Clean Code with AI
Cursor rules for Python development. Enforces PEP 8 style, type hints, docstrings, pytest patterns, and modern Python 3.12+ idioms.
What it is
This is a set of Cursor rules tailored for Python development. It configures Cursor's AI assistant to enforce PEP 8 style, type hints, docstrings, pytest patterns, and modern Python 3.12+ idioms when generating or editing Python code.
Python developers using Cursor as their AI-assisted IDE benefit from these rules by getting consistently clean, idiomatic code suggestions without manually correcting style violations after every generation.
How it saves time or tokens
Without rules, AI code assistants generate code that varies in style and quality. You spend tokens on follow-up prompts asking for type hints, docstrings, or style corrections. These Cursor rules front-load those preferences, so the first generation matches your standards. Fewer correction rounds means fewer tokens consumed.
How to use
- Copy the rules configuration into your project's
.cursorrulesfile. - Open the project in Cursor and start coding. The AI assistant will follow the rules automatically.
- Adjust rules as needed for project-specific conventions (framework preferences, test style).
Example
{
"rules": [
"Always use type hints for function parameters and return values",
"Follow PEP 8 naming: snake_case for functions and variables, PascalCase for classes",
"Write Google-style docstrings for all public functions",
"Use pytest fixtures instead of setUp/tearDown methods",
"Prefer pathlib.Path over os.path for file operations",
"Use f-strings for string formatting, never % or .format()",
"Use match/case for complex conditionals (Python 3.10+)",
"Import from __future__ import annotations for forward references"
]
}
Related on TokRepo
- AI Tools for Coding — AI-powered coding assistants and developer tools
- Prompt Library — Curated prompts and rules for AI workflows
Common pitfalls
- Making rules too specific to one framework, which confuses the AI when you work on non-framework code in the same project.
- Adding contradictory rules (e.g., 'always use classes' and 'prefer functional style') that produce inconsistent output.
- Not testing rules with actual code generation to verify they produce the expected behavior.
Frequently Asked Questions
The rules target Python 3.12+ and use modern idioms like match/case statements, type union syntax (X | Y), and pathlib. You can adjust version-specific rules if your project targets an earlier Python version.
These rules are formatted for Cursor's .cursorrules file. The concepts apply to other AI coding tools, but the configuration format differs. You would need to adapt them for VS Code with Copilot or other editors.
Yes. These rules guide AI code generation, while ruff catches remaining violations at lint time. Using both creates a two-layer quality gate: AI generates clean code, and the linter catches edge cases.
Cursor rules affect all AI-generated code including autocomplete suggestions, inline edits, and chat responses. The rules are injected as context for every AI interaction in the project.
Cursor does not support per-file rule overrides natively. You can use conditional rules like 'In test files, prefer pytest.mark.parametrize for data-driven tests' to adjust behavior based on file context.
Citations (3)
- PEP 8— PEP 8 style guide for Python code
- Cursor Documentation— Cursor AI IDE with rules support
- Python Documentation— Python 3.12 new features and idioms
Related on TokRepo
Source & Thanks
Curated from the cursor.directory community. Based on Python community best practices.
Discussion
Related Assets
Conda — Cross-Platform Package and Environment Manager
Install, update, and manage packages and isolated environments for Python, R, C/C++, and hundreds of other languages from a single tool.
Sphinx — Python Documentation Generator
Generate professional documentation from reStructuredText and Markdown with cross-references, API autodoc, and multiple output formats.
Neutralinojs — Lightweight Cross-Platform Desktop Apps
Build desktop applications with HTML, CSS, and JavaScript using a tiny native runtime instead of bundling Chromium.