You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
These rules apply to application code, tests, migrations, and review comments for this FastAPI service.
Stack and architecture
Use Python 3.11 or newer, FastAPI, Pydantic v2, SQLAlchemy 2.x style, pytest, and type hints.
Keep routers thin. Put business rules in service modules and persistence logic in repository or data-access modules.
Use dependency injection for database sessions, settings, clients, authorization context, and clock access.
Treat API schemas as boundary objects. Do not return ORM entities directly from endpoints.
Keep configuration in typed settings. Never read ad hoc environment variables deep inside business logic.
API correctness
Define explicit request and response models for every endpoint. Use field validators and constrained types where they express real invariants.
Return correct HTTP status codes. Use 400 for malformed input, 401 for missing authentication, 403 for authenticated denial, 404 for missing objects, 409 for conflicts, and 422 for validation failures.
Do not leak exception messages, SQL fragments, stack traces, internal identifiers, or secret values in error responses.
Make mutation endpoints idempotent where retries are expected. Use idempotency keys, unique constraints, or natural keys as appropriate.
Enforce object-level authorization inside service logic. Never rely only on route grouping or frontend hiding.
Document pagination limits and use cursor pagination for large or frequently changing collections.
Async and external IO
Use async endpoints only when they perform non-blocking IO. Do not wrap CPU-heavy work in async functions and call that parallelism.
Never perform blocking network, file, or database calls directly in the event loop. Use proper async clients or controlled thread offload.
Every external call needs a timeout. Retries require backoff, a maximum attempt count, and proof that the operation is idempotent.
Cancel or clean up background tasks explicitly. Log task failure with correlation context.
Data and transactions
Use explicit transactions for multi-write operations. Do not assume autocommit behavior preserves invariants.
Put database constraints behind important invariants: uniqueness, foreign keys, checks, and not-null rules.
Avoid N+1 queries. Use selectinload, joinedload, or explicit queries based on measured access patterns.
Migrations must be reversible or have a documented forward-recovery path. Separate destructive schema changes from backfills when production compatibility matters.
Never trust client-supplied sort, filter, or column names. Map them through an allowlist.
Security and privacy
Validate and normalize input at the API boundary. Keep raw user input out of shell commands, SQL fragments, file paths, template rendering, and URL construction.
Store secrets only in approved secret stores or local untracked environment files. Never print them in logs, tests, exceptions, or examples.
Use constant-time comparisons where secret comparison is required and vetted cryptography libraries for cryptographic operations.
Log security-relevant events without sensitive payloads: authentication failure, authorization denial, ownership changes, administrative actions, and destructive operations.
Testing and verification
Add or update pytest tests for every behavior change. Cover success, validation failure, authorization denial, conflict behavior, and one meaningful edge case.
Use httpx AsyncClient or FastAPI TestClient for API tests. Use transactional rollback or isolated test databases for persistence tests.
Mock only external service boundaries. Do not mock the database behavior being tested unless the test is explicitly a unit test of adaptation logic.
Control clocks and randomness in tests. Avoid sleeps and flaky timing dependencies.
Before finishing, run the narrowest relevant tests first, then the broader suite when practical. Report exact commands and results.
Change discipline
Read existing neighboring code before editing. Follow established project patterns unless the user explicitly asks for a migration.
Make the smallest complete change. Do not reformat unrelated files or mix refactoring with behavior changes.
If a requirement is ambiguous and a wrong guess could change data, security, or public API behavior, ask before implementing.