pydantic中关于属性必填和选填的区别

写法 是否必填 默认值 允许传 None 类型安全
attr: str ✔️ ✔️
attr: str = ... ✔️ ✔️
attr: str = Field(...) ✔️ ✔️
attr: Optional[str] ✔️ ✔️ ✔️
attr: Optional[str] = Field(...) ✔️ ✔️ ✔️
attr: Optional[str] = None None ✔️ ✔️
attr: Optional[str] = Field(None) None ✔️ ✔️
posted @ 2025-05-19 23:27  网无忌  阅读(27)  评论(0)    收藏  举报