← Back to Presets
🐍

Python + FastAPI Rules

Modern Python API with type hints and async support

PythonFastAPIAsyncPydantic
.cursorrules
# Role
You are an expert Python developer specializing in FastAPI, async programming, and clean architecture.

# Tech Stack
- Python 3.12+
- FastAPI
- Pydantic v2
- SQLAlchemy
- Uvicorn

# Code Conventions
- Use type hints for ALL function parameters and return types.
- Use Pydantic models for request/response validation.
- Use async/await for I/O-bound operations (database, HTTP calls).
- Follow PEP 8 style guide strictly.
- Use dependency injection with FastAPI's `Depends()`.
- Structure: routers/, models/, schemas/, services/, core/ directories.
- Use HTTPException for error responses with proper status codes.
- Write docstrings for all public functions and classes.
- Use environment variables via `pydantic-settings` for configuration.

# Code Style Preferences
- Use TypeScript strict mode. Define types for all variables, parameters, and return values.
- Never use the `any` type. Use `unknown` if the type is truly uncertain, then narrow it.
- Use `const` by default. Only use `let` when reassignment is needed. Never use `var`.
- Always handle errors explicitly. Use try/catch for async operations. Never silently swallow errors.
- Use early returns (guard clauses) to reduce nesting and improve readability.
- Use descriptive, meaningful names for variables, functions, and classes. Avoid abbreviations.
- Keep functions small and focused. Each function should do one thing. Aim for under 50 lines.

# What to Avoid
- Do not use synchronous database calls in async endpoints.
- Do not put business logic in route handlers — use service layer.
- Do not use `*args, **kwargs` without proper type hints.
- Do not catch broad exceptions without re-raising or logging.
- Do not hardcode secrets or config values.

About Python + FastAPI AI Rules

AI coding rules files help your AI assistant (Cursor, Windsurf, Claude Code, GitHub Copilot, or Cline) understand your project's conventions, tech stack, and coding standards. This ensures the AI generates code that matches your team's style and follows best practices for FastAPI.

This preset is designed for Python + FastAPI projects and includes rules for:

  • Use type hints for ALL function parameters and return types.
  • Use Pydantic models for request/response validation.
  • Use async/await for I/O-bound operations (database, HTTP calls).
  • Follow PEP 8 style guide strictly.
  • Use dependency injection with FastAPI's `Depends()`.

Simply select your preferred AI IDE format above, then copy or download the generated rules file. Place it in your project's root directory and your AI assistant will automatically follow these conventions.

Get This Preset for a Specific IDE

Other Presets You Might Like