知识卡片
类型标注接入查询参数
书中 FastAPI 示例把查询参数写进端点函数签名,并用类型标注交给框架解析和校验。cancelled: Optional[bool] = None 与 limit: Optional[int] = None 既表达字段类型,也允许调用者不传;处理函数随后可依是否传值决定过滤方式。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
原书出处
参考来源
- 位置:《Microservice APIs》第 6 章《Building REST APIs with Python》(源文件:_epub-src/OEBPS/Text/06.htm)
- 结论依据:原文明确说将查询参数放入函数签名、用类型提示附加校验,并用 Optional 与 None 表示可选。
- 原始内容:All we need to do is include them in the endpoint’s function signature and use type hints to add validation rules for them. Since the query parameters are optional, we’ll mark them as such using the Optional type, and we’ll set their default values to None .