Pydantic Types
The types module contains custom types used by pydantic.
Bases: PydanticMetadata, BaseMetadata
A field metadata class to indicate that a field should be validated in strict mode.
Whether to validate the field in strict mode.
Type: bool
Bases: PydanticMetadata
A field metadata class to indicate that a field should allow -inf, inf, and nan.
Bases: GroupedMetadata
Apply constraints to str types.
Whether to strip whitespace from the string.
Type: bool | None
Whether to convert the string to uppercase.
Type: bool | None
Whether to convert the string to lowercase.
Type: bool | None
Whether to validate the string in strict mode.
Type: bool | None
The minimum length of the string.
Type: int | None
The maximum length of the string.
Type: int | None
A regex pattern that the string must match.
Type: str | None
A type that can be used to import a type from a string.
A special type wrapper which loads JSON before parsing.
Bases: _SecretField[str]
A string that is displayed as ********** in reprs and can be used for passwords.
Bases: _SecretField[bytes]
A bytes that is displayed as ********** in reprs and can be used for passwords.
Bases: str
Based on: https://en.wikipedia.org/wiki/Payment_card_number.
Mask all but the last 4 digits of the card number.
Type: str
@classmethod
def validate(
cls,
__input_value: str,
_: core_schema.ValidationInfo,
) -> PaymentCardNumber
Validate the card number and return a PaymentCardNumber instance.
@classmethod
def validate_digits(cls, card_number: str) -> None
Validate that the card number is all digits.
None
@classmethod
def validate_luhn_check_digit(cls, card_number: str) -> str
Based on: https://en.wikipedia.org/wiki/Luhn_algorithm.
str
@staticmethod
def validate_brand(card_number: str) -> PaymentCardBrand
Validate length based on BIN for major brands: https://en.wikipedia.org/wiki/Payment_card_number#Issuer_identification_number_(IIN).
PaymentCardBrand
Bases: int
Converts a bytes string with units to the number of bytes.
def human_readable(decimal: bool = False) -> str
Converts a byte size to a human readable string.
str — A human readable string representation of the byte size.
If True, use decimal units (e.g. 1000 bytes per KB). If False, use binary units (e.g. 1024 bytes per KiB).
def to(unit: str) -> float
Converts a byte size to another unit.
float — The byte size in the new unit.
The unit to convert to. Must be one of the following: B, KB, MB, GB, TB, PB, EiB, KiB, MiB, GiB, TiB, PiB, EiB.
A date in the past.
A date in the future.
A datetime that requires timezone info.
A datetime that doesn’t require timezone info.
A datetime that must be in the past.
A datetime that must be in the future.
Bases: Protocol
Protocol for encoding and decoding data to and from bytes.
@classmethod
def decode(cls, data: bytes) -> bytes
Decode the data using the encoder.
bytes — The decoded data.
The data to decode.
@classmethod
def encode(cls, value: bytes) -> bytes
Encode the data using the encoder.
bytes — The encoded data.
The data to encode.
@classmethod
def get_json_format(cls) -> str
Get the JSON format for the encoded data.
str — The JSON format for the encoded data.
Bases: EncoderProtocol
Base64 encoder.
@classmethod
def decode(cls, data: bytes) -> bytes
Decode the data from base64 encoded bytes to original bytes data.
bytes — The decoded data.
The data to decode.
@classmethod
def encode(cls, value: bytes) -> bytes
Encode the data from bytes to a base64 encoded bytes.
bytes — The encoded data.
The data to encode.
@classmethod
def get_json_format(cls) -> Literal['base64']
Get the JSON format for the encoded data.
Literal[‘base64’] — The JSON format for the encoded data.
A bytes type that is encoded and decoded using the specified encoder.
def decode(data: bytes, _: core_schema.ValidationInfo) -> bytes
Decode the data using the specified encoder.
bytes — The decoded data.
The data to decode.
def encode(value: bytes) -> bytes
Encode the data using the specified encoder.
bytes — The encoded data.
The data to encode.
Bases: EncodedBytes
A str type that is encoded and decoded using the specified encoder.
def decode_str(data: bytes, _: core_schema.ValidationInfo) -> str
Decode the data using the specified encoder.
str — The decoded data.
The data to decode.
def encode_str(value: str) -> str
Encode the data using the specified encoder.
str — The encoded data.
The data to encode.
A convenience class for creating an annotation that provides pydantic custom type hooks.
This class is intended to eliminate the need to create a custom “marker” which defines the
__get_pydantic_core_schema__ and __get_pydantic_json_schema__ custom hook methods.
For example, to have a field treated by type checkers as int, but by pydantic as Any, you can do:
from typing import Any
from typing_extensions import Annotated
from pydantic import BaseModel, GetPydanticSchema
HandleAsAny = GetPydanticSchema(lambda _s, h: h(Any))
class Model(BaseModel):
x: Annotated[int, HandleAsAny] # pydantic sees `x: Any`
print(repr(Model(x='abc').x))
#> 'abc'
def conint(
strict: bool | None = None,
gt: int | None = None,
ge: int | None = None,
lt: int | None = None,
le: int | None = None,
multiple_of: int | None = None,
) -> type[int]
A wrapper around int that allows for additional constraints.
type[int] — The wrapped integer type.
Whether to validate the integer in strict mode. Defaults to None.
The value must be greater than this.
The value must be greater than or equal to this.
The value must be less than this.
The value must be less than or equal to this.
The value must be a multiple of this.
def confloat(
strict: bool | None = None,
gt: float | None = None,
ge: float | None = None,
lt: float | None = None,
le: float | None = None,
multiple_of: float | None = None,
allow_inf_nan: bool | None = None,
) -> type[float]
A wrapper around float that allows for additional constraints.
type[float] — The wrapped float type.
Whether to validate the float in strict mode.
The value must be greater than this.
The value must be greater than or equal to this.
The value must be less than this.
The value must be less than or equal to this.
The value must be a multiple of this.
Whether to allow -inf, inf, and nan.
def conbytes(
min_length: int | None = None,
max_length: int | None = None,
strict: bool | None = None,
) -> type[bytes]
A wrapper around bytes that allows for additional constraints.
type[bytes] — The wrapped bytes type.
The minimum length of the bytes.
The maximum length of the bytes.
Whether to validate the bytes in strict mode.
def constr(
strip_whitespace: bool | None = None,
to_upper: bool | None = None,
to_lower: bool | None = None,
strict: bool | None = None,
min_length: int | None = None,
max_length: int | None = None,
pattern: str | None = None,
) -> type[str]
A wrapper around str that allows for additional constraints.
type[str] — The wrapped string type.
Whether to strip whitespace from the string.
Whether to convert the string to uppercase.
Whether to convert the string to lowercase.
Whether to validate the string in strict mode.
The minimum length of the string.
The maximum length of the string.
A regex pattern that the string must match.
def conset(
item_type: type[HashableItemType],
min_length: int | None = None,
max_length: int | None = None,
) -> type[set[HashableItemType]]
A wrapper around typing.Set that allows for additional constraints.
type[set[HashableItemType]] — The wrapped set type.
The type of the items in the set.
The minimum length of the set.
The maximum length of the set.
def confrozenset(
item_type: type[HashableItemType],
min_length: int | None = None,
max_length: int | None = None,
) -> type[frozenset[HashableItemType]]
A wrapper around typing.FrozenSet that allows for additional constraints.
type[frozenset[HashableItemType]] — The wrapped frozenset type.
The type of the items in the frozenset.
The minimum length of the frozenset.
The maximum length of the frozenset.
def conlist(
item_type: type[AnyItemType],
min_length: int | None = None,
max_length: int | None = None,
unique_items: bool | None = None,
) -> type[list[AnyItemType]]
A wrapper around typing.List that adds validation.
type[list[AnyItemType]] — The wrapped list type.
The type of the items in the list.
The minimum length of the list. Defaults to None.
The maximum length of the list. Defaults to None.
Whether the items in the list must be unique. Defaults to None.
def condecimal(
strict: bool | None = None,
gt: int | Decimal | None = None,
ge: int | Decimal | None = None,
lt: int | Decimal | None = None,
le: int | Decimal | None = None,
multiple_of: int | Decimal | None = None,
max_digits: int | None = None,
decimal_places: int | None = None,
allow_inf_nan: bool | None = None,
) -> type[Decimal]
A wrapper around Decimal that adds validation.
type[Decimal]
Whether to validate the value in strict mode. Defaults to None.
The value must be greater than this. Defaults to None.
The value must be greater than or equal to this. Defaults to None.
The value must be less than this. Defaults to None.
The value must be less than or equal to this. Defaults to None.
The value must be a multiple of this. Defaults to None.
The maximum number of digits. Defaults to None.
The number of decimal places. Defaults to None.
Whether to allow infinity and NaN. Defaults to None.
def condate(
strict: bool | None = None,
gt: date | None = None,
ge: date | None = None,
lt: date | None = None,
le: date | None = None,
) -> type[date]
A wrapper for date that adds constraints.
type[date] — A date type with the specified constraints.
Whether to validate the date value in strict mode. Defaults to None.
The value must be greater than this. Defaults to None.
The value must be greater than or equal to this. Defaults to None.
The value must be less than this. Defaults to None.
The value must be less than or equal to this. Defaults to None.
A boolean that must be either True or False.
Default: Annotated[bool, Strict()]
An integer that must be greater than zero.
Default: Annotated[int, annotated_types.Gt(0)]
An integer that must be less than zero.
Default: Annotated[int, annotated_types.Lt(0)]
An integer that must be less than or equal to zero.
Default: Annotated[int, annotated_types.Le(0)]
An integer that must be greater than or equal to zero.
Default: Annotated[int, annotated_types.Ge(0)]
An integer that must be validated in strict mode.
Default: Annotated[int, Strict()]
A float that must be greater than zero.
Default: Annotated[float, annotated_types.Gt(0)]
A float that must be less than zero.
Default: Annotated[float, annotated_types.Lt(0)]
A float that must be less than or equal to zero.
Default: Annotated[float, annotated_types.Le(0)]
A float that must be greater than or equal to zero.
Default: Annotated[float, annotated_types.Ge(0)]
A float that must be validated in strict mode.
Default: Annotated[float, Strict(True)]
A float that must be finite (not -inf, inf, or nan).
Default: Annotated[float, AllowInfNan(False)]
A bytes that must be validated in strict mode.
Default: Annotated[bytes, Strict()]
A string that must be validated in strict mode.
Default: Annotated[str, Strict()]
A UUID1 annotated type.
Default: Annotated[UUID, UuidVersion(1)]
A UUID3 annotated type.
Default: Annotated[UUID, UuidVersion(3)]
A UUID4 annotated type.
Default: Annotated[UUID, UuidVersion(4)]
A UUID5 annotated type.
Default: Annotated[UUID, UuidVersion(5)]
A path that must point to a file.
Default: Annotated[Path, PathType('file')]
A path that must point to a directory.
Default: Annotated[Path, PathType('dir')]
A path for a new file or directory that must not already exist.
Default: Annotated[Path, PathType('new')]