FastAPI Code Review
Quick Reference
| Issue Type | Reference |
|---|
| APIRouter setup, responsemodel, status codes | references/routes.md |
| Depends(), yield deps, cleanup, shared deps |
references/dependencies.md |
| Pydantic models, HTTPException, 422 handling |
references/validation.md |
| Async handlers, blocking I/O, background tasks |
references/async.md |
Review Checklist
- - [ ] APIRouter with proper prefix and tags
- [ ] All routes specify
response_model for type safety - [ ] Correct HTTP methods (GET, POST, PUT, DELETE, PATCH)
- [ ] Proper status codes (200, 201, 204, 404, etc.)
- [ ] Dependencies use
Depends() not manual calls - [ ] Yield dependencies have proper cleanup
- [ ] Request/Response models use Pydantic
- [ ] HTTPException with status code and detail
- [ ] All route handlers are INLINECODE2
- [ ] No blocking I/O (
requests, time.sleep, open()) - [ ] Background tasks for non-blocking operations
- [ ] No bare
except in route handlers
Valid Patterns (Do NOT Flag)
These are idiomatic FastAPI patterns that may appear problematic but are correct:
- - Pydantic validates request body automatically - No manual validation needed when using typed Pydantic models as parameters
- Dependency injection for database sessions - Sessions come from
Depends(), not passed as function arguments - HTTPException for all HTTP errors - FastAPI handles conversion to proper HTTP responses
- Async def endpoint without await - May be using sync dependencies or simple operations; FastAPI handles this
- Type annotation on Depends() - This is documentation/IDE support, not a type assertion
- Query/Path/Body defaults - FastAPI processes these at runtime, not traditional Python defaults
- Returning dict from endpoint - Pydantic converts automatically if
response_model is set
Context-Sensitive Rules
Only flag issues when the context warrants it:
- - Flag missing validation ONLY IF the field isn't already in a Pydantic model with validators
- Flag missing auth ONLY IF the endpoint isn't using
Depends() with an auth dependency - Flag missing error handling ONLY IF HTTPException isn't raised appropriately for error cases
- Flag sync in async ONLY IF the operation is actually blocking (file I/O, network calls, CPU-bound), not just non-async
FastAPI Framework Behaviors
FastAPI + Pydantic handle many concerns automatically:
- - Request validation via Pydantic models
- Response serialization via response_model
- Dependency injection for cross-cutting concerns
- Exception handling via exception handlers
Before flagging "missing" functionality, verify FastAPI isn't handling it.
When to Load References
- - Reviewing route definitions → routes.md
- Reviewing dependency injection → dependencies.md
- Reviewing Pydantic models/validation → validation.md
- Reviewing async route handlers → async.md
Review Questions
- 1. Do all routes have explicit response models and status codes?
- Are dependencies injected via Depends() with proper cleanup?
- Do all Pydantic models validate inputs correctly?
- Are all route handlers async and non-blocking?
Before Submitting Findings
Load and follow review-verification-protocol before reporting any issue.
FastAPI 代码审查
快速参考
references/dependencies.md |
| Pydantic 模型、HTTPException、422 处理 |
references/validation.md |
| 异步处理器、阻塞 I/O、后台任务 |
references/async.md |
审查清单
- - [ ] APIRouter 使用正确的前缀和标签
- [ ] 所有路由指定了 response_model 以确保类型安全
- [ ] 正确的 HTTP 方法(GET、POST、PUT、DELETE、PATCH)
- [ ] 正确的状态码(200、201、204、404 等)
- [ ] 依赖使用 Depends() 而非手动调用
- [ ] yield 依赖有正确的清理逻辑
- [ ] 请求/响应模型使用 Pydantic
- [ ] HTTPException 包含状态码和详细信息
- [ ] 所有路由处理器为 async def
- [ ] 无阻塞 I/O(requests、time.sleep、open())
- [ ] 非阻塞操作使用后台任务
- [ ] 路由处理器中无裸 except
有效模式(请勿标记)
以下为符合 FastAPI 习惯用法的模式,可能看似有问题但实际正确:
- - Pydantic 自动验证请求体 - 使用类型化的 Pydantic 模型作为参数时无需手动验证
- 数据库会话的依赖注入 - 会话来自 Depends(),而非作为函数参数传递
- 所有 HTTP 错误使用 HTTPException - FastAPI 处理转换为正确的 HTTP 响应
- 无 await 的 async def 端点 - 可能使用同步依赖或简单操作;FastAPI 可处理
- Depends() 上的类型注解 - 这是文档/IDE 支持,而非类型断言
- Query/Path/Body 默认值 - FastAPI 在运行时处理,非传统 Python 默认值
- 从端点返回字典 - 如果设置了 response_model,Pydantic 会自动转换
上下文敏感规则
仅在上下文需要时标记问题:
- - 标记缺少验证 仅当字段不在包含验证器的 Pydantic 模型中时
- 标记缺少认证 仅当端点未使用包含认证依赖的 Depends() 时
- 标记缺少错误处理 仅当错误情况未适当引发 HTTPException 时
- 标记异步中的同步 仅当操作确实阻塞(文件 I/O、网络调用、CPU 密集型),而非仅非异步
FastAPI 框架行为
FastAPI + Pydantic 自动处理许多关注点:
- - 通过 Pydantic 模型进行请求验证
- 通过 response_model 进行响应序列化
- 横切关注点的依赖注入
- 通过异常处理器进行异常处理
在标记缺少功能前,请确认 FastAPI 未处理该功能。
何时加载参考文档
- - 审查路由定义 → routes.md
- 审查依赖注入 → dependencies.md
- 审查 Pydantic 模型/验证 → validation.md
- 审查异步路由处理器 → async.md
审查问题
- 1. 所有路由是否都有明确的响应模型和状态码?
- 依赖是否通过 Depends() 注入并正确清理?
- 所有 Pydantic 模型是否正确验证输入?
- 所有路由处理器是否为异步且非阻塞?
提交发现前
在报告任何问题前,请加载并遵循 review-verification-protocol。