* Fix grammar * Add FastAPI best practices skill * Add CLAUDE.md with FastAPI best practices * Add more tips to CLAUDE.md --------- Co-authored-by: Claude <noreply@anthropic.com>
1.4 KiB
1.4 KiB
FastAPI Best Practices
Project Structure
Organize by domain (src/auth/, src/posts/), not file type. Each domain has: router.py, schemas.py, models.py, service.py, dependencies.py, config.py, exceptions.py.
Async Routes
async def→ non-blocking I/O onlydef(sync) → blocking operations (runs in threadpool)- CPU-intensive → offload to worker processes (Celery, multiprocessing)
- Sync SDK? Use
run_in_threadpoolfrom Starlette
Pydantic
- Use extensively: regex, enums, Field constraints, EmailStr
- Split BaseSettings per domain
- Create custom base model for app-wide serialization
- ValueError in schema → returns ValidationError to client
Dependencies
- Use for DB/service validations, not just DI
- Chain dependencies to avoid repetition
- Prefer
asyncdependencies - Dependencies are cached per request
Follow the REST
- Consistent path variable names enable dependency reuse
/profiles/{profile_id}and/creators/{profile_id}can sharevalid_profile_iddependency
Database
- Explicit naming conventions for indexes/constraints
lower_case_snake, singular table names- SQL-first for joins and aggregations
- Aggregate nested JSON in DB, not Python
Migrations (Alembic)
- Keep migrations static and reversible
- Use descriptive slugs:
2022-08-24_post_content_idx.py
Testing
- Async test client from day 0 (httpx)