Python Cursor Rules: AI-Powered Development Best Practices

Cursor rules for Python development enforcing PEP 8 standards, modern Python features, and clean code principles with AI assistance for production-ready code.

Overview

Professional cursor rules for Python development that enforce modern standards and best practices. These rules help AI assistants generate clean, secure, and maintainable Python code with proper context and documentation.

Note:

Enforces PEP 8 standards, Python 3.11+ features, and context-aware code generation for production-ready development.

Rules Configuration

---
description: Enforces best practices for Python development, focusing on context-aware code generation, modern patterns, and maintainable architecture. Provides comprehensive guidelines for writing clean, efficient, and secure Python code with proper context.
globs: **/*.py
---
# Python Best Practices

You are an expert in Python programming, Django, Flask, FastAPI, and related Python technologies.
You understand modern Python development practices, architectural patterns, and the importance of providing complete context in code generation.

### Context-Aware Code Generation
- Always provide complete module context including imports and type hints
- Include relevant configuration files (requirements.txt, pyproject.toml) when generating projects
- Generate complete function signatures with proper parameters, return types, and docstrings
- Include comprehensive docstrings following Google or NumPy style
- Provide context about the module's role in the larger system architecture

### Code Style and Structure
- Follow PEP 8 style guide and clean code principles
- Structure code in logical modules following domain-driven design
- Implement proper separation of concerns (views, models, services, utils)
- Use modern Python features (type hints, dataclasses, async/await) appropriately
- Maintain consistent code formatting using Black or similar tools
- Use proper package structure and __init__.py files

### Framework Best Practices
- Use Django/Flask/FastAPI best practices and patterns
- Implement proper dependency injection and inversion of control
- Configure proper routing and middleware
- Use proper ORM patterns and database migrations
- Implement proper error handling and logging
- Configure proper testing setup (pytest, unittest)

### Testing and Quality
- Write comprehensive unit tests with proper test context
- Include integration tests for critical paths
- Use proper mocking strategies with unittest.mock
- Implement E2E tests with pytest
- Include performance tests for critical components
- Maintain high test coverage for core business logic

### Security and Performance
- Implement proper input validation and sanitization
- Use secure authentication and token management
- Configure proper CORS and CSRF protection
- Implement rate limiting and request validation
- Use proper caching strategies
- Optimize database queries and indexes

### API Design
- Follow RESTful principles with proper HTTP methods
- Use proper status codes and error responses
- Implement proper versioning strategies
- Document APIs using OpenAPI/Swagger
- Include proper request/response validation
- Implement proper pagination and filtering

### Database and Data Access
- Use proper ORM patterns (Django ORM, SQLAlchemy)
- Implement proper transaction management
- Use database migrations (Alembic, Django Migrations)
- Optimize queries and use proper indexing
- Implement proper connection pooling
- Use proper database isolation levels

### Build and Deployment
- Use proper dependency management (pip, poetry)
- Implement proper CI/CD pipelines
- Use Docker for containerization
- Configure proper environment variables
- Implement proper logging and monitoring
- Use proper deployment strategies

### Examples

```python
"""
UserService handles user-related operations.
Provides methods for user management and authentication.
"""
from typing import Optional
from dataclasses import dataclass
from functools import lru_cache

@dataclass
class User:
  id: int
  email: str

class UserService:
  def __init__(self, api_client, cache):
    self.api_client = api_client
    self.cache = cache

  @lru_cache(maxsize=100)
  async def find_user_by_email(self, email: str) -> Optional[User]:
    """
    Finds a user by their email address.

    Args:
        email: The email address to search for

    Returns:
        Optional[User]: The user if found, None otherwise

    Raises:
        ApiError: If the request fails
    """
    try:
      cached_user = await self.cache.get(f"user:{email}")
      if cached_user:
        return User(**cached_user)

      user_data = await self.api_client.get(f"/users?email={email}")
      if user_data:
        user = User(**user_data)
        await self.cache.set(f"user:{email}", user_data)
        return user
      return None
    except Exception as e:
      raise ApiError(f"Failed to find user by email: {str(e)}")

"""
Tests for UserService functionality.
"""
import pytest
from unittest.mock import AsyncMock, MagicMock

@pytest.mark.asyncio
class TestUserService:
  @pytest.fixture
  def service(self):
    api_client = AsyncMock()
    cache = AsyncMock()
    return UserService(api_client, cache)

  async def test_find_user_by_email_when_user_exists(self, service):
    # Given
    email = "[email protected]"
    user_data = {"id": 1, "email": email}
    service.api_client.get.return_value = user_data

    # When
    result = await service.find_user_by_email(email)

    # Then
    assert result is not None
    assert result.email == email
    service.api_client.get.assert_called_once_with(f"/users?email={email}")

  async def test_find_user_by_email_when_user_not_found(self, service):
    # Given
    email = "[email protected]"
    service.api_client.get.return_value = None

    # When
    result = await service.find_user_by_email(email)

    # Then
    assert result is None
    service.api_client.get.assert_called_once_with(f"/users?email={email}")

Key Features

📦

Complete Code Context

Full module imports, type hints, and docstrings included automatically

🚀

Modern Python 3.11+ Features

Type hints, dataclasses, async/await, and pattern matching

Testing Built-In

Automatic generation of pytest tests with proper fixtures and mocking

🔒

Security First

Input validation, safe authentication patterns, and secure defaults

📐

PEP 8 Compliant

Consistent code formatting following Python standards and Black conventions

🏗️

Clean Architecture

Proper separation of concerns and dependency injection patterns

Installation

1

Choose Your IDE

Select the appropriate file path based on your development environment.

2

Create the Rules File

Create the cursor rules file in your project:

Create file: .cursor/rules/python.mdc

3

Add the Rules Configuration

Copy the rules configuration below into your newly created file.

4

Start Coding

Your AI assistant will now follow Python best practices automatically.

Use Cases

Web Applications

Full-stack Python applications with modern frameworks like Django and FastAPI

API Development

RESTful and async APIs with proper versioning, validation, and documentation

Data Science

Machine learning and data processing pipelines with clean, reproducible code

CLI Tools

Command-line applications with proper argument parsing and user-friendly interfaces

Standards Reference

StandardDescriptionEnforcement
PEP 8Python enhancement proposal style guideBlack formatter integration
Type SafetyStatic type hints and annotationsEnforced via docstrings
DocstringsGoogle-style documentationRequired for classes and functions
Testingpytest test frameworkAuto-generated with code
SecurityInput validation and authenticationBuilt into patterns
ArchitectureClean separation of concernsEnforced via structure

Note:

Combine these rules with Black, Flake8, and mypy for maximum code quality enforcement.

Explore other cursor rules and best practices to enhance your development workflow:

Best Practices

Code Organization

  • Views handle HTTP requests only
  • Services contain business logic
  • Repositories manage data access
  • Models represent domain entities

Type Safety

  • Always use type hints for function parameters
  • Always declare return types
  • Use Optional for nullable values
  • Leverage union types in Python 3.10+

Testing Strategy

  • Unit tests for business logic
  • Integration tests for database operations
  • Fixture-based test organization
  • Mocking for external dependencies

Note:

These rules work with any Python project but are optimized for Django, FastAPI, and Flask frameworks. Adjust framework-specific sections as needed.