类型
Pydantic 使用类型来定义如何执行验证和序列化。内置和标准库类型(例如 int
、str
、date
)可以按原样使用。严格模式可以控制,并且可以对其应用约束。
在此之上,Pydantic 提供了额外的类型,它们要么直接在库中(例如SecretStr
),要么在 pydantic-extra-types
外部库中。这些都是使用自定义类型部分中描述的模式实现的。严格模式和约束**不能**应用于它们。
内置和标准库类型文档介绍了支持的类型:允许的值、可能的验证约束以及是否可以配置严格模式。
另请参阅转换表,了解每种类型的允许值的摘要。
本页将介绍如何定义自己的自定义类型。
自定义类型¶
有几种方法可以定义您的自定义类型。
使用注解模式¶
注解模式可以用于使类型在您的代码库中可重用。例如,要创建一个表示正整数的类型
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 Schema
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
只允许应用于注解类型本身的元数据(例如验证约束和 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”验证器:您会得到一个 source_type
(不一定与类相同,特别是对于泛型)和一个 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 Schema 的更多详细信息,请参阅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.generate_schema
来为它们生成模式。请注意,我们没有做类似 handler(get_args(source_type)[0])
的操作,因为我们希望为该泛型参数生成一个不相关的模式,而不是受 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 collections.abc import Sequence
from typing import Any, 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 collections.abc import Sequence
from typing import Any, TypeVar
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)
)
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'>