Skip to main content
rule

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.

Install into Cursor · Manually: put the files in place All methods and platforms ↓
.cursor/rules/python-cursorrules-prompt-file-best-practices.mdc
Open on GitHub

What 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 uv and 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.mdc

Frontmatter: description, globs, alwaysApply

Step 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

Similar IDE rules

All IDE rules for Programming languages →

From the same set68

qwik-basic-cursorrules-prompt-file A Cursor rule that sets Qwik conventions for the agent: lazy functions with the $ suffix, signals, tasks, and a TypeScript plus Vite project layout. ★ 41,011repo stars python A Cursor rule that sets project layout, code style and stack choices for Python work with Flask and SQLite. Useful when the agent edits files in your repo. ★ 41,011repo stars python-github-setup-cursorrules-prompt-file Tells Cursor how to write and review Python code: style, tests, GitHub Pull Requests and GitHub Actions CI. Useful when the team has no shared rulebook. ★ 41,011repo stars python-fastapi-best-practices-cursorrules-prompt-f A Cursor rule that sets the agent's coding style for Python and FastAPI: functional code, Pydantic v2, async operations. ★ 41,011repo stars pyspark-etl-best-practices-cursorrules-prompt-file A Cursor rule that sets PySpark code style: F.col(), explicit joins, select over withColumn, and Iceberg-aware ETL structure. ★ 41,011repo stars project-epic-template-cursorrules-prompt-file This rule turns Cursor into a product manager: the agent writes epics and user stories with acceptance criteria and success metrics. ★ 41,011repo stars playwright-defect-tracking-cursorrules-prompt-file A Cursor rule that makes the agent write Playwright tests tied to manual case IDs, with screenshots and logs as defect evidence. ★ 41,011repo stars plasticode-telegram-api-cursorrules-prompt-file A Cursor rule that points the assistant at PHP 7.4, Plasticode and the Telegram Bot API. Useful when your bot is built on Plasticode and you want PSR-12 code. ★ 41,011repo stars

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor