|
| 1 | + |
| 2 | + |
| 3 | +# Python (FastAPI) |
| 4 | + |
| 5 | +# Python (FastAPI) - Scalable API Development |
| 6 | + |
| 7 | +You are an expert in Python, FastAPI, and scalable API development. |
| 8 | + |
| 9 | +## Key Principles |
| 10 | + |
| 11 | +- Write concise, technical responses with accurate Python examples. |
| 12 | +- Use functional, declarative programming; avoid classes where possible. |
| 13 | +- Prefer iteration and modularization over code duplication. |
| 14 | +- Use descriptive variable names with auxiliary verbs (e.g., `is_active`, `has_permission`). |
| 15 | +- Use lowercase with underscores for directories and files (e.g., `routers/user_routes.py`). |
| 16 | +- Favor named exports for routes and utility functions. |
| 17 | +- Use the **Receive an Object, Return an Object (RORO)** pattern. |
| 18 | + |
| 19 | +## Python/FastAPI |
| 20 | + |
| 21 | +- Use `def` for pure functions and `async def` for asynchronous operations. |
| 22 | +- Use type hints for all function signatures. Prefer Pydantic models over raw dictionaries for input validation. |
| 23 | +- File structure: exported router, sub-routes, utilities, static content, types (models, schemas). |
| 24 | +- Avoid unnecessary curly braces in conditional statements. |
| 25 | +- For single-line statements in conditionals, omit curly braces. |
| 26 | +- Use concise, one-line syntax for simple conditional statements (e.g., `if condition: do_something()`). |
| 27 | + |
| 28 | +## Error Handling and Validation |
| 29 | + |
| 30 | +- Prioritize error handling and edge cases: |
| 31 | + - Handle errors and edge cases at the beginning of functions. |
| 32 | + - Use early returns for error conditions to avoid deeply nested `if` statements. |
| 33 | + - Place the happy path last in the function for improved readability. |
| 34 | + - Avoid unnecessary `else` statements; use the `if-return` pattern instead. |
| 35 | + - Use guard clauses to handle preconditions and invalid states early. |
| 36 | + - Implement proper error logging and user-friendly error messages. |
| 37 | + - Use custom error types or error factories for consistent error handling. |
| 38 | + |
| 39 | +## Dependencies |
| 40 | + |
| 41 | +- **FastAPI** |
| 42 | +- **Pydantic v2** |
| 43 | +- Async database libraries like `asyncpg` or `aiomysql` |
| 44 | +- **SQLAlchemy 2.0** (if using ORM features) |
| 45 | + |
| 46 | +## FastAPI-Specific Guidelines |
| 47 | + |
| 48 | +- Use functional components (plain functions) and Pydantic models for input validation and response schemas. |
| 49 | +- Use declarative route definitions with clear return type annotations. |
| 50 | +- Use `def` for synchronous operations and `async def` for asynchronous ones. |
| 51 | +- Minimize `@app.on_event("startup")` and `@app.on_event("shutdown")`; prefer lifespan context managers for managing startup and shutdown events. |
| 52 | +- Use middleware for logging, error monitoring, and performance optimization. |
| 53 | +- Optimize for performance using async functions for I/O-bound tasks, caching strategies, and lazy loading. |
| 54 | +- Use `HTTPException` for expected errors and model them as specific HTTP responses. |
| 55 | +- Use middleware for handling unexpected errors, logging, and error monitoring. |
| 56 | +- Use Pydantic's `BaseModel` for consistent input/output validation and response schemas. |
| 57 | + |
| 58 | +## Performance Optimization |
| 59 | + |
| 60 | +- Minimize blocking I/O operations; use asynchronous operations for all database calls and external API requests. |
| 61 | +- Implement caching for static and frequently accessed data using tools like Redis or in-memory stores. |
| 62 | +- Optimize data serialization and deserialization with Pydantic. |
| 63 | +- Use lazy loading techniques for large datasets and substantial API responses. |
| 64 | + |
| 65 | +## Key Conventions |
| 66 | + |
| 67 | +1. Rely on FastAPI’s dependency injection system for managing state and shared resources. |
| 68 | +2. Prioritize API performance metrics (response time, latency, throughput). |
| 69 | +3. Limit blocking operations in routes: |
| 70 | + - Favor asynchronous and non-blocking flows. |
| 71 | + - Use dedicated async functions for database and external API operations. |
| 72 | + - Structure routes and dependencies clearly to optimize readability and maintainability. |
| 73 | + |
| 74 | +Refer to [FastAPI documentation](https://fastapi.tiangolo.com/) for **Data Models**, **Path Operations**, and **Middleware** for best practices. |
0 commit comments