类型
在可能的情况下,Pydantic 使用 标准库类型 来定义字段,从而简化学习曲线。然而,对于许多有用的应用程序,没有标准库类型存在,因此 Pydantic 实现了许多常用类型。
还有更复杂的类型可以在 Pydantic Extra Types 包中找到。
如果没有现有类型适合您的目的,您还可以实现您自己的 与 Pydantic 兼容的类型,并具有自定义属性和验证。
以下部分描述了 Pydantic 支持的类型。
- 标准库类型 — 来自 Python 标准库的类型。
- 严格类型 — 使您能够防止从兼容类型进行强制转换的类型。
- 自定义数据类型 — 创建您自己的自定义数据类型。
- 字段类型转换 — 不同字段类型之间严格和宽松的转换。
类型转换¶
在验证期间,Pydantic 可以将数据强制转换为预期类型。
有两种强制转换模式:严格和宽松。请参阅 转换表 以了解有关 Pydantic 如何在严格和宽松模式下转换数据的更多详细信息。
有关启用严格强制转换的详细信息,请参阅 严格模式 和 严格类型。
严格类型¶
Pydantic 提供了以下严格类型
这些类型仅当验证的值是相应类型或该类型的子类型时才会通过验证。
约束类型¶
此行为也通过约束类型的 strict
字段公开,并且可以与多种复杂的验证规则结合使用。有关支持的参数,请参阅各个类型签名。
以下注意事项适用
StrictBytes
(以及conbytes()
的strict
选项)将接受bytes
和bytearray
类型。StrictInt
(以及conint()
的strict
选项)将不接受bool
类型,即使bool
是 Python 中int
的子类。其他子类将起作用。StrictFloat
(以及confloat()
的strict
选项)将不接受int
。
除了上述内容外,您还可以使用 FiniteFloat
类型,该类型仅接受有限值(即,不是 inf
、-inf
或 nan
)。
自定义类型¶
您还可以定义自己的自定义数据类型。有几种方法可以实现它。
使用 annotated 模式¶
annotated 模式 可用于使类型在您的代码库中可重用。例如,要创建表示正整数的类型
from typing import Annotated
from pydantic import Field, TypeAdapter, ValidationError
PositiveInt = Annotated[int, Field(gt=0)] # (1)!
ta = TypeAdapter(PositiveInt)
print(ta.validate_python(1))
#> 1
try:
ta.validate_python(-1)
except ValidationError as exc:
print(exc)
"""
1 validation error for constrained-int
Input should be greater than 0 [type=greater_than, input_value=-1, input_type=int]
"""
- 请注意,您还可以使用来自 annotated-types 库的约束,使此操作与 Pydantic 无关
from annotated_types import Gt PositiveInt = Annotated[int, Gt(0)]
添加验证和序列化¶
您可以使用 Pydantic 导出的标记,向任意类型添加或覆盖验证、序列化和 JSON 模式
from typing import Annotated
from pydantic import (
AfterValidator,
PlainSerializer,
TypeAdapter,
WithJsonSchema,
)
TruncatedFloat = Annotated[
float,
AfterValidator(lambda x: round(x, 1)),
PlainSerializer(lambda x: f'{x:.1e}', return_type=str),
WithJsonSchema({'type': 'string'}, mode='serialization'),
]
ta = TypeAdapter(TruncatedFloat)
input = 1.02345
assert input != 1.0
assert ta.validate_python(input) == 1.0
assert ta.dump_json(input) == b'"1.0e+00"'
assert ta.json_schema(mode='validation') == {'type': 'number'}
assert ta.json_schema(mode='serialization') == {'type': 'string'}
泛型¶
from typing import Annotated, TypeVar
from annotated_types import Gt, Len
from pydantic import TypeAdapter, ValidationError
T = TypeVar('T')
ShortList = Annotated[list[T], Len(max_length=4)]
ta = TypeAdapter(ShortList[int])
v = ta.validate_python([1, 2, 3, 4])
assert v == [1, 2, 3, 4]
try:
ta.validate_python([1, 2, 3, 4, 5])
except ValidationError as exc:
print(exc)
"""
1 validation error for list[int]
List should have at most 4 items after validation, not 5 [type=too_long, input_value=[1, 2, 3, 4, 5], input_type=list]
"""
PositiveList = list[Annotated[T, Gt(0)]]
ta = TypeAdapter(PositiveList[float])
v = ta.validate_python([1.0])
assert type(v[0]) is float
try:
ta.validate_python([-1.0])
except ValidationError as exc:
print(exc)
"""
1 validation error for list[constrained-float]
0
Input should be greater than 0 [type=greater_than, input_value=-1.0, input_type=float]
"""
命名类型别名¶
以上示例使用了隐式类型别名,赋值给变量。在运行时,Pydantic 无法知道它被赋值给的变量的名称,这可能会导致两个问题
- 别名的 JSON Schema 不会转换为 定义。当您在模型定义中多次使用别名时,这尤其有用。
- 在大多数情况下,递归类型别名 将不起作用。
通过利用新的 type
语句(在 PEP 695 中引入),您可以按如下方式定义别名
from typing import Annotated
from annotated_types import Gt
from typing_extensions import TypeAliasType
from pydantic import BaseModel
PositiveIntList = TypeAliasType('PositiveIntList', list[Annotated[int, Gt(0)]])
class Model(BaseModel):
x: PositiveIntList
y: PositiveIntList
print(Model.model_json_schema()) # (1)!
"""
{
'$defs': {
'PositiveIntList': {
'items': {'exclusiveMinimum': 0, 'type': 'integer'},
'type': 'array',
}
},
'properties': {
'x': {'$ref': '#/$defs/PositiveIntList'},
'y': {'$ref': '#/$defs/PositiveIntList'},
},
'required': ['x', 'y'],
'title': 'Model',
'type': 'object',
}
"""
- 如果
PositiveIntList
被定义为隐式类型别名,则其定义将在'x'
和'y'
中重复。
from typing import Annotated
from annotated_types import Gt
from pydantic import BaseModel
type PositiveIntList = list[Annotated[int, Gt(0)]]
class Model(BaseModel):
x: PositiveIntList
y: PositiveIntList
print(Model.model_json_schema()) # (1)!
"""
{
'$defs': {
'PositiveIntList': {
'items': {'exclusiveMinimum': 0, 'type': 'integer'},
'type': 'array',
}
},
'properties': {
'x': {'$ref': '#/$defs/PositiveIntList'},
'y': {'$ref': '#/$defs/PositiveIntList'},
},
'required': ['x', 'y'],
'title': 'Model',
'type': 'object',
}
"""
- 如果
PositiveIntList
被定义为隐式类型别名,则其定义将在'x'
和'y'
中重复。
何时使用命名类型别名
虽然(命名)PEP 695 和隐式类型别名旨在对于静态类型检查器是等效的,但 Pydantic 不会理解命名别名内部的字段特定元数据。也就是说,诸如 alias
、default
、deprecated
之类的元数据不能使用
from typing import Annotated
from typing_extensions import TypeAliasType
from pydantic import BaseModel, Field
MyAlias = TypeAliasType('MyAlias', Annotated[int, Field(default=1)])
class Model(BaseModel):
x: MyAlias # This is not allowed
from typing import Annotated
from pydantic import BaseModel, Field
type MyAlias = Annotated[int, Field(default=1)]
class Model(BaseModel):
x: MyAlias # This is not allowed
仅允许可以应用于 annotated 类型本身的元数据(例如 验证约束 和 JSON 元数据)。尝试支持字段特定的元数据将需要急切地检查类型别名的 __value__
,因此 Pydantic 将无法将别名存储为 JSON Schema 定义。
注意
与隐式类型别名一样,类型变量 也可以在泛型别名内部使用
from typing import Annotated, TypeVar
from annotated_types import Len
from typing_extensions import TypeAliasType
T = TypeVar('T')
ShortList = TypeAliasType(
'ShortList', Annotated[list[T], Len(max_length=4)], type_params=(T,)
)
from typing import Annotated, TypeVar
from annotated_types import Len
type ShortList[T] = Annotated[list[T], Len(max_length=4)]
命名递归类型¶
当您需要定义递归类型别名 (1) 时,应使用命名类型别名。
- 由于多种原因,Pydantic 无法支持隐式递归别名。例如,它将无法跨模块解析 前向注解。
例如,这是一个 JSON 类型定义的示例
from typing import Union
from typing_extensions import TypeAliasType
from pydantic import TypeAdapter
Json = TypeAliasType(
'Json',
'Union[dict[str, Json], list[Json], str, int, float, bool, None]', # (1)!
)
ta = TypeAdapter(Json)
print(ta.json_schema())
"""
{
'$defs': {
'Json': {
'anyOf': [
{
'additionalProperties': {'$ref': '#/$defs/Json'},
'type': 'object',
},
{'items': {'$ref': '#/$defs/Json'}, 'type': 'array'},
{'type': 'string'},
{'type': 'integer'},
{'type': 'number'},
{'type': 'boolean'},
{'type': 'null'},
]
}
},
'$ref': '#/$defs/Json',
}
"""
- 用引号括起来注解是必要的,因为它会被急切地评估(并且
Json
尚未定义)。
from pydantic import TypeAdapter
type Json = dict[str, Json] | list[Json] | str | int | float | bool | None # (1)!
ta = TypeAdapter(Json)
print(ta.json_schema())
"""
{
'$defs': {
'Json': {
'anyOf': [
{
'additionalProperties': {'$ref': '#/$defs/Json'},
'type': 'object',
},
{'items': {'$ref': '#/$defs/Json'}, 'type': 'array'},
{'type': 'string'},
{'type': 'integer'},
{'type': 'number'},
{'type': 'boolean'},
{'type': 'null'},
]
}
},
'$ref': '#/$defs/Json',
}
"""
- 命名类型别名的值是延迟评估的,因此无需使用前向注解。
提示
Pydantic 定义了 JsonValue
类型,以提供便利。
使用 __get_pydantic_core_schema__
自定义验证 ¶
为了更广泛地自定义 Pydantic 如何处理自定义类,特别是当您可以访问该类或可以对其进行子类化时,您可以实现特殊的 __get_pydantic_core_schema__
来告诉 Pydantic 如何生成 pydantic-core
模式。
虽然 pydantic
在内部使用 pydantic-core
来处理验证和序列化,但它是 Pydantic V2 的新 API,因此它是未来最有可能被调整的领域之一,您应该尝试坚持使用内置构造,例如 annotated-types
、pydantic.Field
或 BeforeValidator
等提供的构造。
您可以在自定义类型和旨在放入 Annotated
中的元数据上实现 __get_pydantic_core_schema__
。在这两种情况下,API 都类似于中间件,并且类似于“wrap”验证器的 API:您会获得一个 source_type
(对于泛型而言,它不一定与类相同),以及一个 handler
,您可以调用该 handler
并传递一个类型,以调用 Annotated
中的下一个元数据,或调用到 Pydantic 的内部模式生成中。
最简单的无操作实现是用给定的类型调用处理程序,然后将该结果作为结果返回。您也可以选择在调用处理程序之前修改类型,修改处理程序返回的核心模式,或者完全不调用处理程序。
作为自定义类型的方法¶
以下是一个使用 __get_pydantic_core_schema__
来自定义其验证方式的类型示例。这等效于在 Pydantic V1 中实现 __get_validators__
。
from typing import Any
from pydantic_core import CoreSchema, core_schema
from pydantic import GetCoreSchemaHandler, TypeAdapter
class Username(str):
@classmethod
def __get_pydantic_core_schema__(
cls, source_type: Any, handler: GetCoreSchemaHandler
) -> CoreSchema:
return core_schema.no_info_after_validator_function(cls, handler(str))
ta = TypeAdapter(Username)
res = ta.validate_python('abc')
assert isinstance(res, Username)
assert res == 'abc'
有关如何自定义自定义类型的 JSON 模式的更多详细信息,请参阅 JSON Schema。
作为注解¶
通常,您希望通过不仅仅是泛型类型参数来参数化您的自定义类型(您可以通过类型系统来做到这一点,稍后将对此进行讨论)。或者您可能实际上并不关心(或想要)创建您的子类的实例;您实际上想要原始类型,只是做了一些额外的验证。
例如,如果您要自己实现 pydantic.AfterValidator
(请参阅 添加验证和序列化),您将执行类似于以下操作的操作
from dataclasses import dataclass
from typing import Annotated, Any, Callable
from pydantic_core import CoreSchema, core_schema
from pydantic import BaseModel, GetCoreSchemaHandler
@dataclass(frozen=True) # (1)!
class MyAfterValidator:
func: Callable[[Any], Any]
def __get_pydantic_core_schema__(
self, source_type: Any, handler: GetCoreSchemaHandler
) -> CoreSchema:
return core_schema.no_info_after_validator_function(
self.func, handler(source_type)
)
Username = Annotated[str, MyAfterValidator(str.lower)]
class Model(BaseModel):
name: Username
assert Model(name='ABC').name == 'abc' # (2)!
frozen=True
规范使MyAfterValidator
可哈希。没有它,诸如Username | None
之类的联合将引发错误。- 请注意,类型检查器不会像上一个示例那样抱怨将
'ABC'
分配给Username
,因为它们不认为Username
与str
是不同的类型。
from dataclasses import dataclass
from typing import Annotated, Any
from collections.abc import Callable
from pydantic_core import CoreSchema, core_schema
from pydantic import BaseModel, GetCoreSchemaHandler
@dataclass(frozen=True) # (1)!
class MyAfterValidator:
func: Callable[[Any], Any]
def __get_pydantic_core_schema__(
self, source_type: Any, handler: GetCoreSchemaHandler
) -> CoreSchema:
return core_schema.no_info_after_validator_function(
self.func, handler(source_type)
)
Username = Annotated[str, MyAfterValidator(str.lower)]
class Model(BaseModel):
name: Username
assert Model(name='ABC').name == 'abc' # (2)!
frozen=True
规范使MyAfterValidator
可哈希。没有它,诸如Username | None
之类的联合将引发错误。- 请注意,类型检查器不会像上一个示例那样抱怨将
'ABC'
分配给Username
,因为它们不认为Username
与str
是不同的类型。
处理第三方类型¶
前一节中模式的另一个用例是处理第三方类型。
from typing import Annotated, Any
from pydantic_core import core_schema
from pydantic import (
BaseModel,
GetCoreSchemaHandler,
GetJsonSchemaHandler,
ValidationError,
)
from pydantic.json_schema import JsonSchemaValue
class ThirdPartyType:
"""
This is meant to represent a type from a third-party library that wasn't designed with Pydantic
integration in mind, and so doesn't have a `pydantic_core.CoreSchema` or anything.
"""
x: int
def __init__(self):
self.x = 0
class _ThirdPartyTypePydanticAnnotation:
@classmethod
def __get_pydantic_core_schema__(
cls,
_source_type: Any,
_handler: GetCoreSchemaHandler,
) -> core_schema.CoreSchema:
"""
We return a pydantic_core.CoreSchema that behaves in the following ways:
* ints will be parsed as `ThirdPartyType` instances with the int as the x attribute
* `ThirdPartyType` instances will be parsed as `ThirdPartyType` instances without any changes
* Nothing else will pass validation
* Serialization will always return just an int
"""
def validate_from_int(value: int) -> ThirdPartyType:
result = ThirdPartyType()
result.x = value
return result
from_int_schema = core_schema.chain_schema(
[
core_schema.int_schema(),
core_schema.no_info_plain_validator_function(validate_from_int),
]
)
return core_schema.json_or_python_schema(
json_schema=from_int_schema,
python_schema=core_schema.union_schema(
[
# check if it's an instance first before doing any further work
core_schema.is_instance_schema(ThirdPartyType),
from_int_schema,
]
),
serialization=core_schema.plain_serializer_function_ser_schema(
lambda instance: instance.x
),
)
@classmethod
def __get_pydantic_json_schema__(
cls, _core_schema: core_schema.CoreSchema, handler: GetJsonSchemaHandler
) -> JsonSchemaValue:
# Use the same schema that would be used for `int`
return handler(core_schema.int_schema())
# We now create an `Annotated` wrapper that we'll use as the annotation for fields on `BaseModel`s, etc.
PydanticThirdPartyType = Annotated[
ThirdPartyType, _ThirdPartyTypePydanticAnnotation
]
# Create a model class that uses this annotation as a field
class Model(BaseModel):
third_party_type: PydanticThirdPartyType
# Demonstrate that this field is handled correctly, that ints are parsed into `ThirdPartyType`, and that
# these instances are also "dumped" directly into ints as expected.
m_int = Model(third_party_type=1)
assert isinstance(m_int.third_party_type, ThirdPartyType)
assert m_int.third_party_type.x == 1
assert m_int.model_dump() == {'third_party_type': 1}
# Do the same thing where an instance of ThirdPartyType is passed in
instance = ThirdPartyType()
assert instance.x == 0
instance.x = 10
m_instance = Model(third_party_type=instance)
assert isinstance(m_instance.third_party_type, ThirdPartyType)
assert m_instance.third_party_type.x == 10
assert m_instance.model_dump() == {'third_party_type': 10}
# Demonstrate that validation errors are raised as expected for invalid inputs
try:
Model(third_party_type='a')
except ValidationError as e:
print(e)
"""
2 validation errors for Model
third_party_type.is-instance[ThirdPartyType]
Input should be an instance of ThirdPartyType [type=is_instance_of, input_value='a', input_type=str]
third_party_type.chain[int,function-plain[validate_from_int()]]
Input should be a valid integer, unable to parse string as an integer [type=int_parsing, input_value='a', input_type=str]
"""
assert Model.model_json_schema() == {
'properties': {
'third_party_type': {'title': 'Third Party Type', 'type': 'integer'}
},
'required': ['third_party_type'],
'title': 'Model',
'type': 'object',
}
您可以使用此方法来定义例如 Pandas 或 Numpy 类型的行为。
使用 GetPydanticSchema
减少样板代码¶
您可能会注意到,上面我们创建标记类以需要大量样板代码的示例。对于许多简单的情况,您可以通过使用 pydantic.GetPydanticSchema
大大减少这种情况
from typing import Annotated
from pydantic_core import core_schema
from pydantic import BaseModel, GetPydanticSchema
class Model(BaseModel):
y: Annotated[
str,
GetPydanticSchema(
lambda tp, handler: core_schema.no_info_after_validator_function(
lambda x: x * 2, handler(tp)
)
),
]
assert Model(y='ab').y == 'abab'
总结¶
让我们回顾一下
- Pydantic 提供了高级钩子,可以通过
Annotated
(如AfterValidator
和Field
)自定义类型。尽可能使用这些。 - 在底层,这些使用
pydantic-core
来自定义验证,您可以直接使用GetPydanticSchema
或带有__get_pydantic_core_schema__
的标记类来挂钩到该验证。 - 如果您真的想要自定义类型,则可以在类型本身上实现
__get_pydantic_core_schema__
。
处理自定义泛型类¶
警告
这是一种高级技术,您在开始时可能不需要。在大多数情况下,您可能会使用标准的 Pydantic 模型。
您可以使用 泛型类 作为字段类型,并根据“类型参数”(或子类型)使用 __get_pydantic_core_schema__
执行自定义验证。
如果您用作子类型的泛型类具有类方法 __get_pydantic_core_schema__
,则无需使用 arbitrary_types_allowed
即可使其工作。
由于 source_type
参数与 cls
参数不同,因此您可以使用 typing.get_args
(或 typing_extensions.get_args
)提取泛型参数。然后,您可以使用 handler
通过调用 handler.generate_schema
为它们生成模式。请注意,我们不执行类似 handler(get_args(source_type)[0])
的操作,因为我们想要为该泛型参数生成不相关的模式,而不是受 Annotated
元数据等当前上下文影响的模式。这对于自定义类型不太重要,但对于修改模式构建的 annotated 元数据至关重要。
from dataclasses import dataclass
from typing import Any, Generic, TypeVar
from pydantic_core import CoreSchema, core_schema
from typing_extensions import get_args, get_origin
from pydantic import (
BaseModel,
GetCoreSchemaHandler,
ValidationError,
ValidatorFunctionWrapHandler,
)
ItemType = TypeVar('ItemType')
# This is not a pydantic model, it's an arbitrary generic class
@dataclass
class Owner(Generic[ItemType]):
name: str
item: ItemType
@classmethod
def __get_pydantic_core_schema__(
cls, source_type: Any, handler: GetCoreSchemaHandler
) -> CoreSchema:
origin = get_origin(source_type)
if origin is None: # used as `x: Owner` without params
origin = source_type
item_tp = Any
else:
item_tp = get_args(source_type)[0]
# both calling handler(...) and handler.generate_schema(...)
# would work, but prefer the latter for conceptual and consistency reasons
item_schema = handler.generate_schema(item_tp)
def val_item(
v: Owner[Any], handler: ValidatorFunctionWrapHandler
) -> Owner[Any]:
v.item = handler(v.item)
return v
python_schema = core_schema.chain_schema(
# `chain_schema` means do the following steps in order:
[
# Ensure the value is an instance of Owner
core_schema.is_instance_schema(cls),
# Use the item_schema to validate `items`
core_schema.no_info_wrap_validator_function(
val_item, item_schema
),
]
)
return core_schema.json_or_python_schema(
# for JSON accept an object with name and item keys
json_schema=core_schema.chain_schema(
[
core_schema.typed_dict_schema(
{
'name': core_schema.typed_dict_field(
core_schema.str_schema()
),
'item': core_schema.typed_dict_field(item_schema),
}
),
# after validating the json data convert it to python
core_schema.no_info_before_validator_function(
lambda data: Owner(
name=data['name'], item=data['item']
),
# note that we reuse the same schema here as below
python_schema,
),
]
),
python_schema=python_schema,
)
class Car(BaseModel):
color: str
class House(BaseModel):
rooms: int
class Model(BaseModel):
car_owner: Owner[Car]
home_owner: Owner[House]
model = Model(
car_owner=Owner(name='John', item=Car(color='black')),
home_owner=Owner(name='James', item=House(rooms=3)),
)
print(model)
"""
car_owner=Owner(name='John', item=Car(color='black')) home_owner=Owner(name='James', item=House(rooms=3))
"""
try:
# If the values of the sub-types are invalid, we get an error
Model(
car_owner=Owner(name='John', item=House(rooms=3)),
home_owner=Owner(name='James', item=Car(color='black')),
)
except ValidationError as e:
print(e)
"""
2 validation errors for Model
wine
Input should be a valid number, unable to parse string as a number [type=float_parsing, input_value='Kinda good', input_type=str]
cheese
Input should be a valid boolean, unable to interpret input [type=bool_parsing, input_value='yeah', input_type=str]
"""
# Similarly with JSON
model = Model.model_validate_json(
'{"car_owner":{"name":"John","item":{"color":"black"}},"home_owner":{"name":"James","item":{"rooms":3}}}'
)
print(model)
"""
car_owner=Owner(name='John', item=Car(color='black')) home_owner=Owner(name='James', item=House(rooms=3))
"""
try:
Model.model_validate_json(
'{"car_owner":{"name":"John","item":{"rooms":3}},"home_owner":{"name":"James","item":{"color":"black"}}}'
)
except ValidationError as e:
print(e)
"""
2 validation errors for Model
car_owner.item.color
Field required [type=missing, input_value={'rooms': 3}, input_type=dict]
home_owner.item.rooms
Field required [type=missing, input_value={'color': 'black'}, input_type=dict]
"""
from dataclasses import dataclass
from typing import Any, Generic, TypeVar
from pydantic_core import CoreSchema, core_schema
from typing import get_args, get_origin
from pydantic import (
BaseModel,
GetCoreSchemaHandler,
ValidationError,
ValidatorFunctionWrapHandler,
)
ItemType = TypeVar('ItemType')
# This is not a pydantic model, it's an arbitrary generic class
@dataclass
class Owner(Generic[ItemType]):
name: str
item: ItemType
@classmethod
def __get_pydantic_core_schema__(
cls, source_type: Any, handler: GetCoreSchemaHandler
) -> CoreSchema:
origin = get_origin(source_type)
if origin is None: # used as `x: Owner` without params
origin = source_type
item_tp = Any
else:
item_tp = get_args(source_type)[0]
# both calling handler(...) and handler.generate_schema(...)
# would work, but prefer the latter for conceptual and consistency reasons
item_schema = handler.generate_schema(item_tp)
def val_item(
v: Owner[Any], handler: ValidatorFunctionWrapHandler
) -> Owner[Any]:
v.item = handler(v.item)
return v
python_schema = core_schema.chain_schema(
# `chain_schema` means do the following steps in order:
[
# Ensure the value is an instance of Owner
core_schema.is_instance_schema(cls),
# Use the item_schema to validate `items`
core_schema.no_info_wrap_validator_function(
val_item, item_schema
),
]
)
return core_schema.json_or_python_schema(
# for JSON accept an object with name and item keys
json_schema=core_schema.chain_schema(
[
core_schema.typed_dict_schema(
{
'name': core_schema.typed_dict_field(
core_schema.str_schema()
),
'item': core_schema.typed_dict_field(item_schema),
}
),
# after validating the json data convert it to python
core_schema.no_info_before_validator_function(
lambda data: Owner(
name=data['name'], item=data['item']
),
# note that we reuse the same schema here as below
python_schema,
),
]
),
python_schema=python_schema,
)
class Car(BaseModel):
color: str
class House(BaseModel):
rooms: int
class Model(BaseModel):
car_owner: Owner[Car]
home_owner: Owner[House]
model = Model(
car_owner=Owner(name='John', item=Car(color='black')),
home_owner=Owner(name='James', item=House(rooms=3)),
)
print(model)
"""
car_owner=Owner(name='John', item=Car(color='black')) home_owner=Owner(name='James', item=House(rooms=3))
"""
try:
# If the values of the sub-types are invalid, we get an error
Model(
car_owner=Owner(name='John', item=House(rooms=3)),
home_owner=Owner(name='James', item=Car(color='black')),
)
except ValidationError as e:
print(e)
"""
2 validation errors for Model
wine
Input should be a valid number, unable to parse string as a number [type=float_parsing, input_value='Kinda good', input_type=str]
cheese
Input should be a valid boolean, unable to interpret input [type=bool_parsing, input_value='yeah', input_type=str]
"""
# Similarly with JSON
model = Model.model_validate_json(
'{"car_owner":{"name":"John","item":{"color":"black"}},"home_owner":{"name":"James","item":{"rooms":3}}}'
)
print(model)
"""
car_owner=Owner(name='John', item=Car(color='black')) home_owner=Owner(name='James', item=House(rooms=3))
"""
try:
Model.model_validate_json(
'{"car_owner":{"name":"John","item":{"rooms":3}},"home_owner":{"name":"James","item":{"color":"black"}}}'
)
except ValidationError as e:
print(e)
"""
2 validation errors for Model
car_owner.item.color
Field required [type=missing, input_value={'rooms': 3}, input_type=dict]
home_owner.item.rooms
Field required [type=missing, input_value={'color': 'black'}, input_type=dict]
"""
泛型容器¶
相同的想法可以应用于创建泛型容器类型,例如自定义 Sequence
类型
from typing import Any, Sequence, TypeVar
from pydantic_core import ValidationError, core_schema
from typing_extensions import get_args
from pydantic import BaseModel, GetCoreSchemaHandler
T = TypeVar('T')
class MySequence(Sequence[T]):
def __init__(self, v: Sequence[T]):
self.v = v
def __getitem__(self, i):
return self.v[i]
def __len__(self):
return len(self.v)
@classmethod
def __get_pydantic_core_schema__(
cls, source: Any, handler: GetCoreSchemaHandler
) -> core_schema.CoreSchema:
instance_schema = core_schema.is_instance_schema(cls)
args = get_args(source)
if args:
# replace the type and rely on Pydantic to generate the right schema
# for `Sequence`
sequence_t_schema = handler.generate_schema(Sequence[args[0]])
else:
sequence_t_schema = handler.generate_schema(Sequence)
non_instance_schema = core_schema.no_info_after_validator_function(
MySequence, sequence_t_schema
)
return core_schema.union_schema([instance_schema, non_instance_schema])
class M(BaseModel):
model_config = dict(validate_default=True)
s1: MySequence = [3]
m = M()
print(m)
#> s1=<__main__.MySequence object at 0x0123456789ab>
print(m.s1.v)
#> [3]
class M(BaseModel):
s1: MySequence[int]
M(s1=[1])
try:
M(s1=['a'])
except ValidationError as exc:
print(exc)
"""
2 validation errors for M
s1.is-instance[MySequence]
Input should be an instance of MySequence [type=is_instance_of, input_value=['a'], input_type=list]
s1.function-after[MySequence(), json-or-python[json=list[int],python=chain[is-instance[Sequence],function-wrap[sequence_validator()]]]].0
Input should be a valid integer, unable to parse string as an integer [type=int_parsing, input_value='a', input_type=str]
"""
from typing import Any, TypeVar
from collections.abc import Sequence
from pydantic_core import ValidationError, core_schema
from typing import get_args
from pydantic import BaseModel, GetCoreSchemaHandler
T = TypeVar('T')
class MySequence(Sequence[T]):
def __init__(self, v: Sequence[T]):
self.v = v
def __getitem__(self, i):
return self.v[i]
def __len__(self):
return len(self.v)
@classmethod
def __get_pydantic_core_schema__(
cls, source: Any, handler: GetCoreSchemaHandler
) -> core_schema.CoreSchema:
instance_schema = core_schema.is_instance_schema(cls)
args = get_args(source)
if args:
# replace the type and rely on Pydantic to generate the right schema
# for `Sequence`
sequence_t_schema = handler.generate_schema(Sequence[args[0]])
else:
sequence_t_schema = handler.generate_schema(Sequence)
non_instance_schema = core_schema.no_info_after_validator_function(
MySequence, sequence_t_schema
)
return core_schema.union_schema([instance_schema, non_instance_schema])
class M(BaseModel):
model_config = dict(validate_default=True)
s1: MySequence = [3]
m = M()
print(m)
#> s1=<__main__.MySequence object at 0x0123456789ab>
print(m.s1.v)
#> [3]
class M(BaseModel):
s1: MySequence[int]
M(s1=[1])
try:
M(s1=['a'])
except ValidationError as exc:
print(exc)
"""
2 validation errors for M
s1.is-instance[MySequence]
Input should be an instance of MySequence [type=is_instance_of, input_value=['a'], input_type=list]
s1.function-after[MySequence(), json-or-python[json=list[int],python=chain[is-instance[Sequence],function-wrap[sequence_validator()]]]].0
Input should be a valid integer, unable to parse string as an integer [type=int_parsing, input_value='a', input_type=str]
"""
访问字段名称¶
注意
这在 Pydantic V2 到 V2.3 中是不可能的,在 Pydantic V2.4 中 重新添加。
从 Pydantic V2.4 开始,您可以通过 __get_pydantic_core_schema__
中的 handler.field_name
访问字段名称,从而设置字段名称,该字段名称可从 info.field_name
获得。
from typing import Any
from pydantic_core import core_schema
from pydantic import BaseModel, GetCoreSchemaHandler, ValidationInfo
class CustomType:
"""Custom type that stores the field it was used in."""
def __init__(self, value: int, field_name: str):
self.value = value
self.field_name = field_name
def __repr__(self):
return f'CustomType<{self.value} {self.field_name!r}>'
@classmethod
def validate(cls, value: int, info: ValidationInfo):
return cls(value, info.field_name)
@classmethod
def __get_pydantic_core_schema__(
cls, source_type: Any, handler: GetCoreSchemaHandler
) -> core_schema.CoreSchema:
return core_schema.with_info_after_validator_function(
cls.validate, handler(int), field_name=handler.field_name
)
class MyModel(BaseModel):
my_field: CustomType
m = MyModel(my_field=1)
print(m.my_field)
#> CustomType<1 'my_field'>
您还可以从与 Annotated
一起使用的标记中访问 field_name
,例如 AfterValidator
。
from typing import Annotated
from pydantic import AfterValidator, BaseModel, ValidationInfo
def my_validators(value: int, info: ValidationInfo):
return f'<{value} {info.field_name!r}>'
class MyModel(BaseModel):
my_field: Annotated[int, AfterValidator(my_validators)]
m = MyModel(my_field=1)
print(m.my_field)
#> <1 'my_field'>