Pydantic 类型语法解释
Pydantic type syntax explanation
在下面来自官方 FastAPI
tutorials page 的代码中,我无法理解这些语句(例如 name: str
)。
from typing import Optional
from fastapi import FastAPI
from pydantic import BaseModel
class Item(BaseModel):
name: str
description: Optional[str] = None
price: float
tax: Optional[float] = None
app = FastAPI()
@app.post("/items/")
async def create_item(item: Item):
return item
我的意思是,我知道他们应该强制执行类型,但他们应该如何执行,因为 python 不强制执行类型。
- 我还看到了 Pydantic 手册,但在 class 声明中没有看到关于此特定语法(例如
name: str
)的解释。
有人可以为我解释一下这个语法吗?当你以这种方式创建它时,你如何检查 class 的预期类型?
提前致谢。
Python的打字行为
Python 3.5: https://docs.python.org/3/library/typing.html 以上支持打字。
如果您的类型提示不正确,您的代码仍将 运行 - 正如文档所说:它只是一个类型提示。 Python 仍然是一种动态类型的语言。
但是,您可以使用 MyPy 在 运行time.
之前捕获这些错误
Pydantic 的打字行为
尽管了解 Pydantic 的行为也很重要:如果您的类型不是 str,但可以进行转换,它将首先转换为字符串,而不会出现任何错误消息。否则会报错。
如果你想在没有转换的情况下强制引发错误,你应该使用 Pydantic 严格类型:https://pydantic-docs.helpmanual.io/usage/types/#strict-types
但 Pydantic 文档告诉您的是:
“[...] 仅注释声明告诉 pydantic 该字段是必需的。如果可能,字符串、字节或浮点数将被强制转换为整数;否则将引发异常。”
(https://pydantic-docs.helpmanual.io/)
语法“name: str”是Python 3.6 以上的标准特性。这是一个类型提示,实际上并没有改变下面 Python 的基本行为。该变量可以有任何值,但这是一个提示,提醒您您希望它是一个字符串。它还允许像 mypy 这样的 linters 标记您正在调用此变量上的方法,这些方法在 str 上不存在,因此可能会在运行时中断。最后,它允许上下文敏感的编辑器预测可用的方法,因为它有提示这是什么类型的变量,这在 Python
中通常不会
在Python 3.5 they introduced the type hints and it follows a specific syntax (see PEP-484 and PEP-3107).
它指出,
PEP 3107 introduced syntax for function annotations, but the semantics were deliberately left undefined. There has now been enough 3rd party usage for static type analysis that the community would benefit from a standard vocabulary and baseline tools within the standard library.
这意味着 Python 不强制执行验证或静态评估,但某些第 3 方库可以做到这一点。
使用 Pydatic 的 “验证执行技术”,他们编写了如何评估 classes 的逻辑继承自 BaseModel
他们一直在从 __init__(...)
方法本身调用验证器,因此如果输入数据不正确,您将得到 ValidationError
异常满足定义的验证条件。
简而言之,Pydatic 的 BaseModel
是一个普通的 Python class,它采用 __init__(...)
参数并根据定义的 class 变量进行验证。
在下面来自官方 FastAPI
tutorials page 的代码中,我无法理解这些语句(例如 name: str
)。
from typing import Optional
from fastapi import FastAPI
from pydantic import BaseModel
class Item(BaseModel):
name: str
description: Optional[str] = None
price: float
tax: Optional[float] = None
app = FastAPI()
@app.post("/items/")
async def create_item(item: Item):
return item
我的意思是,我知道他们应该强制执行类型,但他们应该如何执行,因为 python 不强制执行类型。
- 我还看到了 Pydantic 手册,但在 class 声明中没有看到关于此特定语法(例如
name: str
)的解释。
有人可以为我解释一下这个语法吗?当你以这种方式创建它时,你如何检查 class 的预期类型?
提前致谢。
Python的打字行为
Python 3.5: https://docs.python.org/3/library/typing.html 以上支持打字。 如果您的类型提示不正确,您的代码仍将 运行 - 正如文档所说:它只是一个类型提示。 Python 仍然是一种动态类型的语言。 但是,您可以使用 MyPy 在 运行time.
之前捕获这些错误Pydantic 的打字行为
尽管了解 Pydantic 的行为也很重要:如果您的类型不是 str,但可以进行转换,它将首先转换为字符串,而不会出现任何错误消息。否则会报错。
如果你想在没有转换的情况下强制引发错误,你应该使用 Pydantic 严格类型:https://pydantic-docs.helpmanual.io/usage/types/#strict-types
但 Pydantic 文档告诉您的是: “[...] 仅注释声明告诉 pydantic 该字段是必需的。如果可能,字符串、字节或浮点数将被强制转换为整数;否则将引发异常。” (https://pydantic-docs.helpmanual.io/)
语法“name: str”是Python 3.6 以上的标准特性。这是一个类型提示,实际上并没有改变下面 Python 的基本行为。该变量可以有任何值,但这是一个提示,提醒您您希望它是一个字符串。它还允许像 mypy 这样的 linters 标记您正在调用此变量上的方法,这些方法在 str 上不存在,因此可能会在运行时中断。最后,它允许上下文敏感的编辑器预测可用的方法,因为它有提示这是什么类型的变量,这在 Python
中通常不会在Python 3.5 they introduced the type hints and it follows a specific syntax (see PEP-484 and PEP-3107).
它指出,
PEP 3107 introduced syntax for function annotations, but the semantics were deliberately left undefined. There has now been enough 3rd party usage for static type analysis that the community would benefit from a standard vocabulary and baseline tools within the standard library.
这意味着 Python 不强制执行验证或静态评估,但某些第 3 方库可以做到这一点。
使用 Pydatic 的 “验证执行技术”,他们编写了如何评估 classes 的逻辑继承自 BaseModel
他们一直在从 __init__(...)
方法本身调用验证器,因此如果输入数据不正确,您将得到 ValidationError
异常满足定义的验证条件。
简而言之,Pydatic 的 BaseModel
是一个普通的 Python class,它采用 __init__(...)
参数并根据定义的 class 变量进行验证。