Back to Blog
PythonFastAPICursor

Essential Cursor Rules for Python & FastAPI Developers

Stop writing boilerplate. These 5 Cursor rules will force your AI to write type-safe, production-ready Python code.

AgentDepot TeamDecember 10, 20256 min read

Essential Cursor Rules for Python & FastAPI

While the Javascript ecosystem gets a lot of AI love, Python developers are silently building massive backends with agents.

If you are using FastAPI, Pydantic, or Django, you need to configure your Cursor rules to handle Python's flexibility without creating a mess.

Rule 1: The "Type Hinting" Enforcer

Python's dynamic nature is a double-edged sword for AI. Force it to be strict.

You are a Python expert.
- ALWAYS use type hints (PEP 484).
- Use 'typing' module or modern python 3.10+ syntax (list[str] | None).
- Never leave function arguments untyped.
- If a variable type is ambiguous, use explicit modification.

Rule 2: FastAPI Best Practices

FastAPI is great, but AI often mixes up Pydantic V1 and V2, or dependency injection patterns.

Context: FastAPI Project
- Use Pydantic V2 (`model_config`, `field_validator`).
- Use `Annotated` for dependency injection: `db: Annotated[Session, Depends(get_db)]`.
- Group routes using APIRouter.
- Always return Pydantic models, not raw dicts.

Rule 3: The Docstring Standard

AI writes great code but terrible docs. Standardize it.

- Use Google Style Python Docstrings.
- Every public function must have a docstring with Args, Returns, and Raises.
- Comments should explain WHY, not WHAT.

Rule 4: Pytest Only

Don't let it use unittest.

- Use `pytest` for all testing.
- Use `conftest.py` for fixtures.
- Avoid class-based tests; use functions.
- Mock external APIs using `respx` or `unittest.mock`.

How to use these

Combine these into your .cursorrules file at the root of your repository. Your AI will go from writing "script-kiddie" Python to senior engineering code instantly.

Download the full Python Rule Pack on AgentDepot →

Find More AI Agents

Explore our directory of AI coding agents, rules, and plugins for Cursor, Windsurf, Claude Code, and more.