Python Backend Rules Pack
The Problem
Cursor generates Python code with no type hints, catches bare Exception, stuffs business logic into route handlers, and mixes sync and async patterns. You spend more time refactoring AI output than writing code yourself.
The Fix
Drop one .cursorrules file into your project root. Cursor immediately starts generating code that follows modern Python backend best practices.
What's Inside
π Python Fundamentals
- Type hints on every function β modern str | None syntax
- Pydantic v2 for all data validation and serialization
- Clean project structure grouped by feature, not by layer
- Proper error handling with custom exception hierarchies
β‘ FastAPI
- Thin route handlers that delegate to service functions
- Dependency injection for auth, DB sessions, and shared resources
- Async patterns done right β httpx over requests, proper connection pools
- Consistent error responses across all endpoints
ποΈ Django
- Model best practices β str, indexes, TextChoices
- select_related and prefetch_related to kill N+1 queries
- DRF serializers, ViewSets, and permission classes
- Paginated list endpoints by default
ποΈ Database
- SQLAlchemy 2.0 with Mapped type annotations
- Alembic for migrations β never create_all() in production
- Eager loading strategies to prevent N+1 queries
- Explicit transactions for write operations
π Security & Testing
- bcrypt/argon2 for passwords, JWT with refresh tokens
- Input validation at every boundary
- pytest with fixtures, factories, and proper mocking
- Error paths tested, not just happy paths
π« Anti-Patterns Blocked
- No more Any types
- No more bare except Exception
- No more business logic in route handlers
- No more sync libraries in async code
- No more hardcoded secrets
Setup
- Download the .cursorrules file
- Drop it in your project root
- Done. Cursor picks it up automatically.
What You Get
.cursorrules file, README, and setup guide. Drop it in your project root and Cursor instantly follows modern Python backend best practices.
.cursorrules file, README, and setup guide. Drop it in your project root and Cursor instantly follows modern Python backend best practices.