See Pydantic Types for other custom IP address types ipaddress.IPv4Network simply uses the type itself for validation by passing the value to IPv4Network(v) See Pydantic Types for other custom IP address types ipaddress.IPv4Interface simply uses the type itself for validation by passing the value to IPv4Address(v) See Infinite Generators below for more detail on parsing and validation typing.Type see Type below for more detail on parsing and validation typing.Callable see Callable below for more detail on parsing and validation typing.Pattern will cause the input value to be passed to re.compile(v) to create a regex pattern ipaddress.IPv4Address simply uses the type itself for validation by passing the value to IPv4Address(v) See Annotated Types below for more detail on parsing and validation typing.Set see Typing Iterables below for more detail on parsing and validation typing.FrozenSet see Typing Iterables below for more detail on parsing and validation typing.Deque see Typing Iterables below for more detail on parsing and validation typing.Sequence see Typing Iterables below for more detail on parsing and validation typing.Iterable this is reserved for iterables that shouldn't be consumed. See Annotated Types below for more detail on parsing and validation subclass of collections.namedtuple Same as subclass of typing.NamedTuple but all fields will have type Any since they are not annotated typing.Dict see Typing Iterables below for more detail on parsing and validation subclass of typing.TypedDict Same as dict but pydantic will validate the dictionary since keys are annotated. typing.List see Typing Iterables below for more detail on parsing and validation typing.Tuple see Typing Iterables below for more detail on parsing and validation subclass of typing.NamedTuple Same as tuple but instantiates with the given namedtuple and validates fields since they are annotated. See Unions below for more detail on parsing and validation and Required Fields for details about required fields that can receive None as a value. typing.TypeVar constrains the values allowed based on constraints or bound, see TypeVar typing.Union see Unions below for more detail on parsing and validation typing.Optional Optional is simply short hand for Union The additional metadata is ignored and the root type is used. TheĪnnotated hint may contain a single call to the Field function, but otherwise See typing.Deque below for sub-type constraints datetime.date see Datetime Types below for more detail on parsing and validation datetime.time see Datetime Types below for more detail on parsing and validation datetime.datetime see Datetime Types below for more detail on parsing and validation datetime.timedelta see Datetime Types below for more detail on parsing and validation typing.Any allows any value including None, thus an Any field is optional typing.Annotated allows wrapping another type with arbitrary metadata, as per PEP-593. See typing.FrozenSet below for sub-type constraints deque allows list, tuple, set, frozenset, deque, or generators and casts to a deque See typing.Set below for sub-type constraints frozenset allows list, tuple, set, frozenset, deque, or generators and casts to a frozen set See typing.Dict below for sub-type constraints set allows list, tuple, set, frozenset, deque, or generators and casts to a set See typing.Tuple below for sub-type constraints dict dict(v) is used to attempt to convert a dictionary See typing.List below for sub-type constraints tuple allows list, tuple, set, frozenset, deque, or generators and casts to a tuple See this warning on loss of information during data conversion float similarly, float(v) is used to coerce values to floats str strings are accepted as-is, int float and Decimal are coerced using str(v), bytes and bytearray areĬonverted using v.decode(), enums inheriting from str are converted using v.value,Īnd all other types cause an error bytes bytes are accepted as-is, bytearray is converted using bytes(v), str are converted using v.encode(),Īnd int, float, and Decimal are coerced using str(v).encode() list allows list, tuple, set, frozenset, deque, or generators and casts to a list None, type(None) or Literal (equivalent according to PEP 484) allows only None value bool see Booleans below for details on how bools are validated and what values are permitted int pydantic uses int(v) to coerce types to an int to require a positive int) seeĬonstrained Types. Strict Types if you need to constrain the values allowed (e.g. Pydantic supports many common types from the Python standard library. If no existing type suits your purpose you can also implement your own pydantic-compatible types So pydantic implements many commonly used types. For many useful applications, however, no standard library type exists, Where possible pydantic uses standard library types to define fields, thus smoothing
0 Comments
Leave a Reply. |