转换表

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

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

字段类型 输入 严格 输入来源 条件
bool bool Python 和 JSON
bool 浮点数 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 必须是精确的日期,例如没有 H, M, S, f
date 浮点数 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 浮点数 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 必须实现映射接口并具有 items() 方法。
dict Object JSON
浮点数 bool Python 和 JSON
浮点数 bytes Python 必须匹配 [0-9]+(\.[0-9]+)?
浮点数 浮点数 Python 和 JSON 显式禁止 bool
浮点数 int Python 和 JSON
浮点数 str Python 和 JSON 必须匹配 [0-9]+(\.[0-9]+)?
浮点数 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 浮点数 Python 和 JSON 必须是精确的整数,例如 val % 1 == 0,对于 nan, inf 会引发错误。
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 浮点数 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 浮点数 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 浮点数 Python 和 JSON
ByteSize int Python 和 JSON
ByteSize str Python 和 JSON
ByteSize Decimal Python
Decimal 浮点数 JSON
Decimal 浮点数 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 必须实现映射接口并具有 items() 方法。
TypedDict Object JSON
UUID str JSON
UUID str Python
UUID UUID Python
字段类型 输入 严格 输入来源 条件
bool bool Python 和 JSON
bool 浮点数 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 浮点数 Python 和 JSON 解释为从 epoch 开始的秒或毫秒。参见 speedate。必须是精确的日期。
date int Python 和 JSON 解释为从 epoch 开始的秒或毫秒。参见 speedate。必须是精确的日期。
date str Python 和 JSON 格式:YYYY-MM-DD
datetime 浮点数 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
浮点数 bool Python 和 JSON
浮点数 浮点数 Python 和 JSON 显式禁止 bool
浮点数 int Python 和 JSON
浮点数 str Python 和 JSON 必须匹配 [0-9]+(\.[0-9]+)?
frozenset Array JSON
int bool Python 和 JSON
int 浮点数 Python 和 JSON 必须是精确的整数,例如 val % 1 == 0,对于 nan, inf 会引发错误。
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 浮点数 Python 和 JSON 解释为秒,范围 0 - 86399.9*
time int Python 和 JSON 解释为秒,范围 0 - 86399
time str Python 和 JSON 格式:HH:MM:SS.FFFFFF。参见 speedate
timedelta 浮点数 Python 和 JSON 解释为秒。
timedelta int Python 和 JSON 解释为秒。
timedelta str Python 和 JSON 格式:ISO8601。参见 speedate
tuple Array JSON
Any Any Python 和 JSON
ByteSize 浮点数 Python 和 JSON
ByteSize int Python 和 JSON
ByteSize str Python 和 JSON
Decimal 浮点数 JSON
Decimal 浮点数 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
浮点数 浮点数 Python 和 JSON 显式禁止 bool
浮点数 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 浮点数 Python 和 JSON
ByteSize int Python 和 JSON
ByteSize str Python 和 JSON
Decimal 浮点数 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 浮点数 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 必须是精确的日期,例如没有 H, M, S, f
date 浮点数 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 浮点数 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 必须实现映射接口并具有 items() 方法。
浮点数 bool Python 和 JSON
浮点数 bytes Python 必须匹配 [0-9]+(\.[0-9]+)?
浮点数 浮点数 Python 和 JSON 显式禁止 bool
浮点数 int Python 和 JSON
浮点数 str Python 和 JSON 必须匹配 [0-9]+(\.[0-9]+)?
浮点数 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 浮点数 Python 和 JSON 必须是精确的整数,例如 val % 1 == 0,对于 nan, inf 会引发错误。
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 浮点数 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 浮点数 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 浮点数 Python 和 JSON
ByteSize int Python 和 JSON
ByteSize str Python 和 JSON
ByteSize Decimal Python
Decimal 浮点数 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 必须实现映射接口并具有 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
浮点数 浮点数 Python 和 JSON 显式禁止 bool
浮点数 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 浮点数 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