ConfigsMar 29, 2026·1 min read

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.

TL;DR
Cursor rules that enforce PEP 8, type hints, pytest patterns, and Python 3.12+ idioms in AI-assisted coding.
§01

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.

§02

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.

§03

How to use

  1. Copy the rules configuration into your project's .cursorrules file.
  2. Open the project in Cursor and start coding. The AI assistant will follow the rules automatically.
  3. Adjust rules as needed for project-specific conventions (framework preferences, test style).
§04

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"
  ]
}
§05

Related on TokRepo

§06

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

What Python version do these rules target?+

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.

Do these rules work with other AI IDEs?+

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.

Can I combine these with a linter like ruff?+

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.

Do the rules affect autocomplete or only chat?+

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.

How do I override rules for specific files?+

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)
🙏

Source & Thanks

Curated from the cursor.directory community. Based on Python community best practices.

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets