Python Best Practices

Python Best Practices

Sets Cursor's Python habits: clear project layout, full typing and PEP 257 docstrings, uv and Ruff, pytest-only fully annotated tests, env-based config and CI.


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

Public domain (CC0 1.0) · by awesome-cursorrules contributors · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 11,517 prompts

More by @OpenSourceBuffGuy

See all
Quiet Luxury Furniture Nameplate Poster Style prompt image

Quiet Luxury Furniture Nameplate Poster Style

Defines a JSON style for premium furniture catalog posters with huge green type behind one hero piece on warm off-white, with fill-in variables.

image-prompt, json-prompt, template, placeholders, product-photography, poster, luxury, minimalist, interior-design, editorial, studio-lighting, negative-space, typography, text-rendering, structured-output, style-reference, for-designers, for-marketers, long-form, vertical-format, widescreen, advanced

Java Development prompt image

Java Development

Guides an AI coding agent through everyday plain Java work: detecting the build setup, using Maven or Gradle wrappers, modern idioms, logging, quality tools and CI, without upgrading anything unasked.

agent-skill, best-practices, code-generation, code-review, java, coding, backend, clean-code, ci-cd, github-actions, security-review, refactoring, for-developers, code, long-form, advanced, unit-testing, performance-optimization, rule-file, checklist, maven, gradle

Session Management prompt image

Session Management

Guides a coding agent to build secure session handling with JWT tokens, refresh flow, logout and CSRF protection across Python and Node.js backends.

agent-skill, authentication, application-security, best-practices, code-generation, backend, python, flask, nodejs, javascript, redis, api, cybersecurity, owasp, checklist, bash, code, for-developers, advanced, long-form, jwt

Reviews

Loading reviews…

Discussion

Loading comments…