Skills2026年3月29日·1 分钟阅读

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.

Agent 就绪

先审查再安装

这个资产需要先审查。复制的指令会要求 Agent dry-run、列出写入项,确认后再继续。

Needs Confirmation · 66/100策略:需确认
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Community
入口
Cursor Rules: Python — Clean Code with AI
先审查命令
npx -y tokrepo@latest install f18133e8-aad7-4875-9828-acc45a09962f --target codex

先 dry-run,确认写入项后再运行此命令。

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.

常见问题

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.

引用来源 (3)
🙏

来源与感谢

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

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产