python-cursorrules-prompt-file-best-practices — Cursor rule for Python
A Cursor rule that makes the assistant write Python with type annotations, PEP 257 docstrings and pytest tests.
.cursor/rules/python-cursorrules-prompt-file-best-practices.mdcWhat python-cursorrules-prompt-file-best-practices does
The file sets boundaries for Python work: separate directories for source code, tests, docs and config, plus a modular layout with distinct files for models, services, controllers and utilities. Configuration goes through environment variables. Dependencies are managed with uv and virtual environments, Ruff keeps the style consistent, and CI runs on GitHub Actions or GitLab CI.
The strict part is the code itself. Every function and class gets type annotations with an explicit return type, None included, and a descriptive docstring following PEP 257. Comments already in a file stay where they are. Tests are written only with pytest or its plugins, never unittest, live under ./tests, carry annotations and docstrings, and packages get an __init__.py. For type checking the rule asks you to import CaptureFixture, FixtureRequest, LogCaptureFixture, MonkeyPatch and MockerFixture.
The rule ships with alwaysApply: false and globs: **/*. So it will not fire on its own — you attach it when you want it. That matters on a large codebase, because once active it starts adding docstrings and annotations to files you had no intention of touching.
What it helps with
- add type annotations and return types to existing functions
- bring docstrings to PEP 257 without dropping existing comments
- move tests from unittest to pytest under
./tests - lay out directories for source, tests, docs and config
- set up dependencies with
uvand linting with Ruff
Example prompts for the agent
- Add type annotations and PEP 257 docstrings to this module, leave the comments alone
- Rewrite these tests with pytest, add annotations and put them under
./tests - Split the project into source, tests, docs and config directories
How to install python-cursorrules-prompt-file-best-practices
Manually: put the files in place
.cursor/rules/python-cursorrules-prompt-file-best-practices.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.claude/rules/python-cursorrules-prompt-file-best-practices.mdThe paths frontmatter field limits the files: move globs from the source into paths
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.github/instructions/python-cursorrules-prompt-file-best-practices.instructions.mdThe applyTo field sets which files it applies to: move globs from the source into applyTo
Manually: put the files in place
.devin/rules/python-cursorrules-prompt-file-best-practices.mdManually: put the files in place
.clinerules/python-cursorrules-prompt-file-best-practices.mdStep by step, with commands for each platform: Cursor rules: .mdc files, four modes and what replaced .cursorrules
Questions about python-cursorrules-prompt-file-best-practices
How do I install python-cursorrules-prompt-file-best-practices in Cursor?
Create .cursor/rules/python-cursorrules-prompt-file-best-practices.mdc in your project root and paste the rule contents into it.
What does python-cursorrules-prompt-file-best-practices do?
It tells the Cursor assistant how to write Python: type annotations, PEP 257 docstrings, pytest-only tests, uv for dependencies and Ruff for linting.
Does the rule apply to every file automatically?
No. It sets alwaysApply: false, so you attach it manually, while globs: **/* defines the scope across the project.
Source text
python-cursorrules-prompt-file-best-practices.mdc · CC0-1.0
--- description: "Cursor rules for Python development with best practices integration." globs: **/* alwaysApply: false --- You are an AI assistant specialized in Python development. Your approach emphasizes: - Clear project structure with separate directories for source code, tests, docs, and config. - Modular design with distinct files for models, services, controllers, and utilities. - Configuration management using environment variables. - Robust error handling and logging, including context capture. - Comprehensive testing with pytest. - Detailed documentation using docstrings and README files. - Dependency management via https://github.com/astral-sh/uv and virtual environments. - Code style consistency using Ruff. - CI/CD implementation with GitHub Actions or GitLab CI. AI-friendly coding practices: - You provide code snippets and explanations tailored to these principles, optimizing for clarity and AI-assisted development. Follow the following rules: - For any Python file, ALWAYS add typing annotations to each function or class. Include explicit return types (including None where appropriate). Add descriptive docstrings to all Python functions and classes. - Please follow PEP 257 docstring conventions. Update existing docstrings as needed. - Make sure you keep any comments that exist in a file. - When writing tests, ONLY use pytest or pytest plugins (not unittest). All tests should have typing annotations. Place all tests under ./tests. Create any necessary directories. If you create packages under ./tests or ./src/<package_name>, be sure to add an __init__.py if one does not exist. All tests should be fully annotated and should contain docstrings. Be sure to import the following if TYPE_CHECKING: from _pytest.capture import CaptureFixture from _pytest.fixtures import FixtureRequest from _pytest.logging import LogCaptureFixture from _pytest.monkeypatch import MonkeyPatch from pytest_mock.plugin import MockerFixture
© PatrickJS · CC0-1.0 · PatrickJS/awesome-cursorrules