转换表

下表详细说明了 Pydantic 在严格和宽松模式下验证期间如何转换数据。

“严格”列包含复选标记,指示在 严格模式下验证时允许的类型转换。

字段类型 输入 严格 输入源 条件
bool bool Python & JSON
bool float Python & JSON 允许的值:0.0, 1.0
bool int Python & JSON 允许的值:0, 1
bool str Python & JSON 允许的值:'f', 'n', 'no', 'off', 'false', 'False', 't', 'y', 'on', 'yes', 'true', 'True'
bool Decimal Python 允许的值:Decimal(0), Decimal(1)
bytes bytearray Python
bytes bytes Python
bytes str JSON
bytes str Python
callable - JSON 永远无效。
callable Any Python callable() 检查必须返回 True
date bytes Python 格式:YYYY-MM-DD (UTF-8)。
date date Python
date datetime Python 必须是精确的日期,例如,没有 HMSf
date float Python & JSON 解释为自 epoch 以来的秒或毫秒。请参阅 speedate。必须是精确的日期。
date int Python & JSON 解释为自 epoch 以来的秒或毫秒。请参阅 speedate。必须是精确的日期。
date str Python & JSON 格式:YYYY-MM-DD
date Decimal Python 解释为自 epoch 以来的秒或毫秒。请参阅 speedate。必须是精确的日期。
datetime bytes Python 格式:YYYY-MM-DDTHH:MM:SS.fYYYY-MM-DD。请参阅 speedate,(UTF-8)。
datetime date Python
datetime datetime Python
datetime float Python & JSON 解释为自 epoch 以来的秒或毫秒,请参阅 speedate
datetime int Python & JSON 解释为自 epoch 以来的秒或毫秒,请参阅 speedate
datetime str Python & JSON 格式:YYYY-MM-DDTHH:MM:SS.fYYYY-MM-DD。请参阅 speedate
datetime Decimal Python 解释为自 epoch 以来的秒或毫秒,请参阅 speedate
deque deque Python
deque frozenset Python
deque list Python
deque set Python
deque tuple Python
deque Array JSON
dict dict Python
dict Mapping Python 必须实现 mapping 接口并具有 items() 方法。
dict Object JSON
float bool Python & JSON
float bytes Python 必须匹配 [0-9]+(\.[0-9]+)?
float float Python & JSON 明确禁止 bool
float int Python & JSON
float str Python & JSON 必须匹配 [0-9]+(\.[0-9]+)?
float Decimal Python
frozenset deque Python
frozenset dict_keys Python
frozenset dict_values Python
frozenset frozenset Python
frozenset list Python
frozenset set Python
frozenset tuple Python
frozenset Array JSON
int bool Python & JSON
int bytes Python 必须仅为数字,例如 [0-9]+
int float Python & JSON 必须是精确的整数,例如 val % 1 == 0,对于 naninf 会引发错误。
int int Python & JSON 明确禁止 bool
int int Python & JSON
int str Python & JSON 必须仅为数字,例如 [0-9]+
int Decimal Python 必须是精确的整数,例如 val % 1 == 0
list deque Python
list dict_keys Python
list dict_values Python
list frozenset Python
list list Python
list set Python
list tuple Python
list Array JSON
namedtuple dict Python
namedtuple list Python
namedtuple namedtuple Python
namedtuple tuple Python
namedtuple Array JSON
namedtuple NamedTuple Python
set deque Python
set dict_keys Python
set dict_values Python
set frozenset Python
set list Python
set set Python
set tuple Python
set Array JSON
str bytearray Python 假定为 UTF-8 编码,unicode 解码错误时会报错。
str bytes Python 假定为 UTF-8 编码,unicode 解码错误时会报错。
str str Python & JSON
time bytes Python 格式:HH:MM:SS.FFFFFF。请参阅 speedate
time float Python & JSON 解释为秒,范围 0 - 86399.9*
time int Python & JSON 解释为秒,范围 0 - 86399
time str Python & JSON 格式:HH:MM:SS.FFFFFF。请参阅 speedate
time time Python
time Decimal Python 解释为秒,范围 0 - 86399.9*
timedelta bytes Python 格式:ISO8601。请参阅 speedate,(UTF-8)。
timedelta float Python & JSON 解释为秒。
timedelta int Python & JSON 解释为秒。
timedelta str Python & JSON 格式:ISO8601。请参阅 speedate
timedelta timedelta Python
timedelta Decimal Python 解释为秒。
tuple deque Python
tuple dict_keys Python
tuple dict_values Python
tuple frozenset Python
tuple list Python
tuple set Python
tuple tuple Python
tuple Array JSON
type type Python
Any Any Python & JSON
ByteSize float Python & JSON
ByteSize int Python & JSON
ByteSize str Python & JSON
ByteSize Decimal Python
Decimal float JSON
Decimal float Python & JSON
Decimal int JSON
Decimal int Python & JSON
Decimal str JSON
Decimal str Python & JSON 必须匹配 [0-9]+(\.[0-9]+)?
Decimal Decimal Python
Enum Any JSON 输入值必须可转换为枚举值。
Enum Any Python 输入值必须可转换为枚举值。
Enum Enum Python
IPv4Address bytes Python
IPv4Address int Python 表示 IP 地址的整数,必须小于 2**32
IPv4Address str JSON
IPv4Address str Python & JSON
IPv4Address IPv4Address Python
IPv4Address IPv4Interface Python
IPv4Interface bytes Python
IPv4Interface int Python 表示 IP 地址的整数,必须小于 2**32
IPv4Interface str JSON
IPv4Interface str Python & JSON
IPv4Interface tuple Python
IPv4Interface IPv4Address Python
IPv4Interface IPv4Interface Python
IPv4Network bytes Python
IPv4Network int Python 表示 IP 网络的整数,必须小于 2**32
IPv4Network str JSON
IPv4Network str Python & JSON
IPv4Network IPv4Address Python
IPv4Network IPv4Interface Python
IPv4Network IPv4Network Python
IPv6Address bytes Python
IPv6Address int Python 表示 IP 地址的整数,必须小于 2**128
IPv6Address str JSON
IPv6Address str Python & JSON
IPv6Address IPv6Address Python
IPv6Address IPv6Interface Python
IPv6Interface bytes Python
IPv6Interface int Python 表示 IP 地址的整数,必须小于 2**128
IPv6Interface str JSON
IPv6Interface str Python & JSON
IPv6Interface tuple Python
IPv6Interface IPv6Address Python
IPv6Interface IPv6Interface Python
IPv6Network bytes Python
IPv6Network int Python 表示 IP 地址的整数,必须小于 2**128
IPv6Network str JSON
IPv6Network str Python & JSON
IPv6Network IPv6Address Python
IPv6Network IPv6Interface Python
IPv6Network IPv6Network Python
InstanceOf - JSON 永远无效。
InstanceOf Any Python isinstance() 检查必须返回 True
IntEnum Any JSON 输入值必须可转换为枚举值。
IntEnum Any Python 输入值必须可转换为枚举值。
IntEnum IntEnum Python
Iterable deque Python
Iterable frozenset Python
Iterable list Python
Iterable set Python
Iterable tuple Python
Iterable Array JSON
NamedTuple dict Python
NamedTuple list Python
NamedTuple namedtuple Python
NamedTuple tuple Python
NamedTuple Array JSON
NamedTuple NamedTuple Python
None None Python & JSON
Path str JSON
Path str Python
Path Path Python
Pattern bytes Python 输入必须是有效的模式。
Pattern str Python & JSON 输入必须是有效的模式。
Sequence deque Python
Sequence list Python
Sequence tuple Python
Sequence Array JSON
TypedDict dict Python
TypedDict Any Python
TypedDict Mapping Python 必须实现 mapping 接口并具有 items() 方法。
TypedDict Object JSON
UUID str JSON
UUID str Python
UUID UUID Python
字段类型 输入 严格 输入源 条件
bool bool Python & JSON
bool float Python & JSON 允许的值:0.0, 1.0
bool int Python & JSON 允许的值:0, 1
bool str Python & JSON 允许的值:'f', 'n', 'no', 'off', 'false', 'False', 't', 'y', 'on', 'yes', 'true', 'True'
bytes str JSON
callable - JSON 永远无效。
date float Python & JSON 解释为自 epoch 以来的秒或毫秒。请参阅 speedate。必须是精确的日期。
date int Python & JSON 解释为自 epoch 以来的秒或毫秒。请参阅 speedate。必须是精确的日期。
date str Python & JSON 格式:YYYY-MM-DD
datetime float Python & JSON 解释为自 epoch 以来的秒或毫秒,请参阅 speedate
datetime int Python & JSON 解释为自 epoch 以来的秒或毫秒,请参阅 speedate
datetime str Python & JSON 格式:YYYY-MM-DDTHH:MM:SS.fYYYY-MM-DD。请参阅 speedate
deque Array JSON
dict Object JSON
float bool Python & JSON
float float Python & JSON 明确禁止 bool
float int Python & JSON
float str Python & JSON 必须匹配 [0-9]+(\.[0-9]+)?
frozenset Array JSON
int bool Python & JSON
int float Python & JSON 必须是精确的整数,例如 val % 1 == 0,对于 naninf 会引发错误。
int int Python & JSON 明确禁止 bool
int int Python & JSON
int str Python & JSON 必须仅为数字,例如 [0-9]+
list Array JSON
namedtuple Array JSON
set Array JSON
str str Python & JSON
time float Python & JSON 解释为秒,范围 0 - 86399.9*
time int Python & JSON 解释为秒,范围 0 - 86399
time str Python & JSON 格式:HH:MM:SS.FFFFFF。请参阅 speedate
timedelta float Python & JSON 解释为秒。
timedelta int Python & JSON 解释为秒。
timedelta str Python & JSON 格式:ISO8601。请参阅 speedate
tuple Array JSON
Any Any Python & JSON
ByteSize float Python & JSON
ByteSize int Python & JSON
ByteSize str Python & JSON
Decimal float JSON
Decimal float Python & JSON
Decimal int JSON
Decimal int Python & JSON
Decimal str JSON
Decimal str Python & JSON 必须匹配 [0-9]+(\.[0-9]+)?
Enum Any JSON 输入值必须可转换为枚举值。
IPv4Address str JSON
IPv4Address str Python & JSON
IPv4Interface str JSON
IPv4Interface str Python & JSON
IPv4Network str JSON
IPv4Network str Python & JSON
IPv6Address str JSON
IPv6Address str Python & JSON
IPv6Interface str JSON
IPv6Interface str Python & JSON
IPv6Network str JSON
IPv6Network str Python & JSON
InstanceOf - JSON 永远无效。
IntEnum Any JSON 输入值必须可转换为枚举值。
Iterable Array JSON
NamedTuple Array JSON
None None Python & JSON
Path str JSON
Pattern str Python & JSON 输入必须是有效的模式。
Sequence Array JSON
TypedDict Object JSON
UUID str JSON
字段类型 输入 严格 输入源 条件
bool bool Python & JSON
bytes str JSON
deque Array JSON
dict Object JSON
float float Python & JSON 明确禁止 bool
float int Python & JSON
frozenset Array JSON
int int Python & JSON 明确禁止 bool
list Array JSON
namedtuple Array JSON
set Array JSON
str str Python & JSON
tuple Array JSON
Any Any Python & JSON
ByteSize float Python & JSON
ByteSize int Python & JSON
ByteSize str Python & JSON
Decimal float JSON
Decimal int JSON
Decimal str JSON
Enum Any JSON 输入值必须可转换为枚举值。
IPv4Address str JSON
IPv4Interface str JSON
IPv4Network str JSON
IPv6Address str JSON
IPv6Interface str JSON
IPv6Network str JSON
IntEnum Any JSON 输入值必须可转换为枚举值。
Iterable Array JSON
NamedTuple Array JSON
None None Python & JSON
Path str JSON
Pattern str Python & JSON 输入必须是有效的模式。
Sequence Array JSON
TypedDict Object JSON
UUID str JSON
字段类型 输入 严格 输入源 条件
bool bool Python & JSON
bool float Python & JSON 允许的值:0.0, 1.0
bool int Python & JSON 允许的值:0, 1
bool str Python & JSON 允许的值:'f', 'n', 'no', 'off', 'false', 'False', 't', 'y', 'on', 'yes', 'true', 'True'
bool Decimal Python 允许的值:Decimal(0), Decimal(1)
bytes bytearray Python
bytes bytes Python
bytes str Python
callable Any Python callable() 检查必须返回 True
date bytes Python 格式:YYYY-MM-DD (UTF-8)。
date date Python
date datetime Python 必须是精确的日期,例如,没有 HMSf
date float Python & JSON 解释为自 epoch 以来的秒或毫秒。请参阅 speedate。必须是精确的日期。
date int Python & JSON 解释为自 epoch 以来的秒或毫秒。请参阅 speedate。必须是精确的日期。
date str Python & JSON 格式:YYYY-MM-DD
date Decimal Python 解释为自 epoch 以来的秒或毫秒。请参阅 speedate。必须是精确的日期。
datetime bytes Python 格式:YYYY-MM-DDTHH:MM:SS.fYYYY-MM-DD。请参阅 speedate,(UTF-8)。
datetime date Python
datetime datetime Python
datetime float Python & JSON 解释为自 epoch 以来的秒或毫秒,请参阅 speedate
datetime int Python & JSON 解释为自 epoch 以来的秒或毫秒,请参阅 speedate
datetime str Python & JSON 格式:YYYY-MM-DDTHH:MM:SS.fYYYY-MM-DD。请参阅 speedate
datetime Decimal Python 解释为自 epoch 以来的秒或毫秒,请参阅 speedate
deque deque Python
deque frozenset Python
deque list Python
deque set Python
deque tuple Python
dict dict Python
dict Mapping Python 必须实现 mapping 接口并具有 items() 方法。
float bool Python & JSON
float bytes Python 必须匹配 [0-9]+(\.[0-9]+)?
float float Python & JSON 明确禁止 bool
float int Python & JSON
float str Python & JSON 必须匹配 [0-9]+(\.[0-9]+)?
float Decimal Python
frozenset deque Python
frozenset dict_keys Python
frozenset dict_values Python
frozenset frozenset Python
frozenset list Python
frozenset set Python
frozenset tuple Python
int bool Python & JSON
int bytes Python 必须仅为数字,例如 [0-9]+
int float Python & JSON 必须是精确的整数,例如 val % 1 == 0,对于 naninf 会引发错误。
int int Python & JSON 明确禁止 bool
int int Python & JSON
int str Python & JSON 必须仅为数字,例如 [0-9]+
int Decimal Python 必须是精确的整数,例如 val % 1 == 0
list deque Python
list dict_keys Python
list dict_values Python
list frozenset Python
list list Python
list set Python
list tuple Python
namedtuple dict Python
namedtuple list Python
namedtuple namedtuple Python
namedtuple tuple Python
namedtuple NamedTuple Python
set deque Python
set dict_keys Python
set dict_values Python
set frozenset Python
set list Python
set set Python
set tuple Python
str bytearray Python 假定为 UTF-8 编码,unicode 解码错误时会报错。
str bytes Python 假定为 UTF-8 编码,unicode 解码错误时会报错。
str str Python & JSON
time bytes Python 格式:HH:MM:SS.FFFFFF。请参阅 speedate
time float Python & JSON 解释为秒,范围 0 - 86399.9*
time int Python & JSON 解释为秒,范围 0 - 86399
time str Python & JSON 格式:HH:MM:SS.FFFFFF。请参阅 speedate
time time Python
time Decimal Python 解释为秒,范围 0 - 86399.9*
timedelta bytes Python 格式:ISO8601。请参阅 speedate,(UTF-8)。
timedelta float Python & JSON 解释为秒。
timedelta int Python & JSON 解释为秒。
timedelta str Python & JSON 格式:ISO8601。请参阅 speedate
timedelta timedelta Python
timedelta Decimal Python 解释为秒。
tuple deque Python
tuple dict_keys Python
tuple dict_values Python
tuple frozenset Python
tuple list Python
tuple set Python
tuple tuple Python
type type Python
Any Any Python & JSON
ByteSize float Python & JSON
ByteSize int Python & JSON
ByteSize str Python & JSON
ByteSize Decimal Python
Decimal float Python & JSON
Decimal int Python & JSON
Decimal str Python & JSON 必须匹配 [0-9]+(\.[0-9]+)?
Decimal Decimal Python
Enum Any Python 输入值必须可转换为枚举值。
Enum Enum Python
IPv4Address bytes Python
IPv4Address int Python 表示 IP 地址的整数,必须小于 2**32
IPv4Address str Python & JSON
IPv4Address IPv4Address Python
IPv4Address IPv4Interface Python
IPv4Interface bytes Python
IPv4Interface int Python 表示 IP 地址的整数,必须小于 2**32
IPv4Interface str Python & JSON
IPv4Interface tuple Python
IPv4Interface IPv4Address Python
IPv4Interface IPv4Interface Python
IPv4Network bytes Python
IPv4Network int Python 表示 IP 网络的整数,必须小于 2**32
IPv4Network str Python & JSON
IPv4Network IPv4Address Python
IPv4Network IPv4Interface Python
IPv4Network IPv4Network Python
IPv6Address bytes Python
IPv6Address int Python 表示 IP 地址的整数,必须小于 2**128
IPv6Address str Python & JSON
IPv6Address IPv6Address Python
IPv6Address IPv6Interface Python
IPv6Interface bytes Python
IPv6Interface int Python 表示 IP 地址的整数,必须小于 2**128
IPv6Interface str Python & JSON
IPv6Interface tuple Python
IPv6Interface IPv6Address Python
IPv6Interface IPv6Interface Python
IPv6Network bytes Python
IPv6Network int Python 表示 IP 地址的整数,必须小于 2**128
IPv6Network str Python & JSON
IPv6Network IPv6Address Python
IPv6Network IPv6Interface Python
IPv6Network IPv6Network Python
InstanceOf Any Python isinstance() 检查必须返回 True
IntEnum Any Python 输入值必须可转换为枚举值。
IntEnum IntEnum Python
Iterable deque Python
Iterable frozenset Python
Iterable list Python
Iterable set Python
Iterable tuple Python
NamedTuple dict Python
NamedTuple list Python
NamedTuple namedtuple Python
NamedTuple tuple Python
NamedTuple NamedTuple Python
None None Python & JSON
Path str Python
Path Path Python
Pattern bytes Python 输入必须是有效的模式。
Pattern str Python & JSON 输入必须是有效的模式。
Sequence deque Python
Sequence list Python
Sequence tuple Python
TypedDict dict Python
TypedDict Any Python
TypedDict Mapping Python 必须实现 mapping 接口并具有 items() 方法。
UUID str Python
UUID UUID Python
字段类型 输入 严格 输入源 条件
bool bool Python & JSON
bytes bytes Python
callable Any Python callable() 检查必须返回 True
date date Python
datetime datetime Python
deque deque Python
dict dict Python
float float Python & JSON 明确禁止 bool
float int Python & JSON
frozenset frozenset Python
int int Python & JSON 明确禁止 bool
list list Python
namedtuple dict Python
namedtuple list Python
namedtuple namedtuple Python
namedtuple tuple Python
namedtuple NamedTuple Python
set set Python
str str Python & JSON
time time Python
timedelta timedelta Python
tuple tuple Python
type type Python
Any Any Python & JSON
ByteSize float Python & JSON
ByteSize int Python & JSON
ByteSize str Python & JSON
ByteSize Decimal Python
Decimal Decimal Python
Enum Enum Python
IPv4Address IPv4Address Python
IPv4Address IPv4Interface Python
IPv4Interface IPv4Interface Python
IPv4Network IPv4Network Python
IPv6Address IPv6Address Python
IPv6Address IPv6Interface Python
IPv6Interface IPv6Interface Python
IPv6Network IPv6Network Python
InstanceOf Any Python isinstance() 检查必须返回 True
IntEnum IntEnum Python
Iterable deque Python
Iterable frozenset Python
Iterable list Python
Iterable set Python
Iterable tuple Python
NamedTuple dict Python
NamedTuple list Python
NamedTuple namedtuple Python
NamedTuple tuple Python
NamedTuple NamedTuple Python
None None Python & JSON
Path Path Python
Pattern bytes Python 输入必须是有效的模式。
Pattern str Python & JSON 输入必须是有效的模式。
Sequence list Python
TypedDict dict Python
TypedDict Any Python
UUID UUID Python