pydantic field types
field types
pydantic 尽可能的使用标准库类型(standard library types)来标注字段来提供一个平滑的学习曲线;不过它也实现了许多常用的类型(commonly used types)
你当然可以创造属于自己的类型(your own pydantic-compatible types)
标准库类型
None,type(None)orLiteral[None]- 只允许
None值
- 只允许
bool- 详见
Booleans
- 详见
int- pydantic 使用
int(v)去将 v 强制转换成int
- pydantic 使用
float- 同样地,
float(v)也会将 v 强制转换成float
- 同样地,
strstr原样接收intfloat和Decimal会被str(v)强制转换bytes和bytearray会用v.decode()强制转换- 继承自
str的enum会被v.value强制转换 - 其他的类型都会报错
bytesbytes原样接收bytearray用bytes(v)转换str用v.encode()转换int,float和Decimal用str(v).encode()强制转换
list允许
list,tuple,set,frozenset,deque, 或者生成器(generators)并且将其转换为 listtupleallows
list,tuple,set,frozenset,deque, or generators and casts to a tuple; seetyping.Tuplebelow for sub-type constraintsdictdict(v)is used to attempt to convert a dictionary; seetyping.Dictbelow for sub-type constraintssetallows
list,tuple,set,frozenset,deque, or generators and casts to a set; seetyping.Setbelow for sub-type constraintsfrozensetallows
list,tuple,set,frozenset,deque, or generators and casts to a frozen set; seetyping.FrozenSetbelow for sub-type constraintsdequeallows
list,tuple,set,frozenset,deque, or generators and casts to a deque; seetyping.Dequebelow for sub-type constraintsdatetime.datesee Datetime Types below for more detail on parsing and validation
datetime.timesee Datetime Types below for more detail on parsing and validation
datetime.datetimesee Datetime Types below for more detail on parsing and validation
datetime.timedeltasee Datetime Types below for more detail on parsing and validation
typing.Anyallows any value including
None, thus anAnyfield is optionaltyping.Annotatedallows wrapping another type with arbitrary metadata, as per PEP-593. The
Annotatedhint may contain a single call to theFieldfunction, but otherwise the additional metadata is ignored and the root type is used.typing.TypeVarconstrains the values allowed based on
constraintsorbound, see TypeVartyping.Unionsee Unions below for more detail on parsing and validation
typing.OptionalOptional[x]is simply short hand forUnion[x, None]; see Unions below for more detail on parsing and validation and Required Fields for details about required fields that can receiveNoneas a value.typing.Listtyping.Tuplesubclass of typing.NamedTupleSame as
tuplebut instantiates with the given namedtuple and validates fields since they are annotated. See Annotated Types below for more detail on parsing and validationsubclass of collections.namedtupleSame as
subclass of typing.NamedTuplebut all fields will have typeAnysince they are not annotatedtyping.Dictsee Typing Iterables below for more detail on parsing and validation
subclass of typing.TypedDictSame as
dictbut pydantic will validate the dictionary since keys are annotated. See Annotated Types below for more detail on parsing and validationtyping.Setsee Typing Iterables below for more detail on parsing and validation
typing.FrozenSetsee Typing Iterables below for more detail on parsing and validation
typing.Dequesee Typing Iterables below for more detail on parsing and validation
typing.Sequencesee Typing Iterables below for more detail on parsing and validation
typing.Iterablethis is reserved for iterables that shouldn’t be consumed. See Infinite Generators below for more detail on parsing and validation
typing.Typesee Type below for more detail on parsing and validation
typing.Callablesee Callable below for more detail on parsing and validation
typing.Patternwill cause the input value to be passed to
re.compile(v)to create a regex patternipaddress.IPv4Addresssimply uses the type itself for validation by passing the value to
IPv4Address(v); see Pydantic Types for other custom IP address typesipaddress.IPv4Interfacesimply uses the type itself for validation by passing the value to
IPv4Address(v); see Pydantic Types for other custom IP address typesipaddress.IPv4Networksimply uses the type itself for validation by passing the value to
IPv4Network(v); see Pydantic Types for other custom IP address typesipaddress.IPv6Addresssimply uses the type itself for validation by passing the value to
IPv6Address(v); see Pydantic Types for other custom IP address typesipaddress.IPv6Interfacesimply uses the type itself for validation by passing the value to
IPv6Interface(v); see Pydantic Types for other custom IP address typesipaddress.IPv6Networksimply uses the type itself for validation by passing the value to
IPv6Network(v); see Pydantic Types for other custom IP address typesenum.Enumchecks that the value is a valid Enum instance
subclass of enum.Enumchecks that the value is a valid member of the enum; see Enums and Choices for more details
enum.IntEnumchecks that the value is a valid IntEnum instance
subclass of enum.IntEnumchecks that the value is a valid member of the integer enum; see Enums and Choices for more details
decimal.Decimalpydantic attempts to convert the value to a string, then passes the string to
Decimal(v)pathlib.Pathsimply uses the type itself for validation by passing the value to
Path(v); see Pydantic Types for other more strict path typesuuid.UUIDstrings and bytes (converted to strings) are passed to
UUID(v), with a fallback toUUID(bytes=v)forbytesandbytearray; see Pydantic Types for other stricter UUID typesByteSizeconverts a bytes string with units to bytes