我正在尝试使用FastAPI编写一个应用程序,该应用程序大量使用pydantic。另外,我想使用mypy检查我的代码类型。我怎样才能在pydantic和mypy中使用类型注解而不会发生冲突?
我知道type: ignore评论,但在我看来,这是一种欺骗:)
示例:
from pydantic import BaseModel, Schema
class UsersQuery(BaseModel):
limit: int = Schema(default=100, gt=0, le=100)
offset: int = Schema(default=0, ge=0)此代码工作正常,但类型检查失败。
mypy输出:
error: Incompatible types in assignment (expression has type "Schema", variable has type "int")
error: Incompatible types in assignment (expression has type "Schema", variable has type "int")发布于 2019-10-22 03:36:45
type: ignore是目前唯一的解决方案。
pydantic的版本1应该在几天内发布,其中Field (它取代了v1中的Schema )是一个返回Any的函数,它应该可以解决这个问题。
tl;dr等待v1发布并得到fastapi的支持,您的问题就应该解决了。
https://stackoverflow.com/questions/58486316
复制相似问题