Skip to content

Documentation

pydantic_pint

Pydantic validation for Pint Quantities.

app_registry module-attribute

app_registry = pint.ApplicationRegistry(_DEFAULT_REGISTRY)

Pydantic Pint default application registry.

PydanticPintQuantity

PydanticPintQuantity(
    _arg=None,
    /,
    *,
    ureg=None,
    ureg_contexts=None,
    restriction=None,
    ser_mode=None,
    ser_mode_json=None,
    strict=True,
    exact=False,
)

Pydantic Pint Quantity.

Pydantic compatible annotation for validating and serializing pint.Quantity fields. Accepts units or dimensions as the restriction type for the field.

PARAMETER DESCRIPTION
_arg

The base units or dimensions to check the Pydantic field. If the field is restricted by units, all input units must be convertible to these units. If the field is restricted by dimensions, then any unit of that dimension is allowed. When this field and restriction is set to None, any units are accepted.

TYPE: str | Mapping[str, int] | None DEFAULT: None

ureg

A custom Pint unit registry. If not specified, the default unit registry from pydantic_pint.registry.app_registry is used. See pydantic_pint.registry.get_registry and pydantic_pint.registry.set_registry.

TYPE: UnitRegistry | None DEFAULT: None

ureg_contexts

A custom Pint context (or context name) for the default unit registry. All contexts are applied in validation conversion.

TYPE: Iterable[str | Context] | None DEFAULT: None

restriction

Identify what the argument is restricting, the units or dimensions. By default, it will automatically determine if the argument is specifying units or dimensions. It is recommended to use the default.

TYPE: Literal['units', 'dimensions'] | None DEFAULT: None

ser_mode

The mode for serializing the field; either "str", "dict", "number". This parameter is used for both Pydantic's serialization modes ("python" or "json") when ser_mode_json is set to None. By default, in Pydantic's "python" serialization mode, fields are serialzied to a pint.Quantity. Note, the units are dropped when serializing to a number.

TYPE: Literal['str', 'dict', 'number'] | None DEFAULT: None

ser_mode_json

The mode for serializing the field specifically for Pydantic's "json" serialization mode; either "str", "dict", "number". This parameter overrides ser_mode when Pydantic serialization mode is set to "json". If ser_mode_json is set to None, the field will use the same value as ser_mode. In the case were ser_mode is also set to None, the field will be serialized to a str in Pydantic's "json" serialization mode. Note, the units are dropped when serializing to a number.

TYPE: Literal['str', 'dict', 'number'] | None DEFAULT: None

strict

Forces users to specify units; on by default. If disabled, a value without units - provided by the user - will be treated as the base units of the PydanticPintQuantity. Strict mode is ignored and always applied if specifying dimensionality (instead of units).

TYPE: bool DEFAULT: True

exact

Forces the units to be exact; off by default. If enabled, a value with units - provided by the user - must match the base units of the PydanticPintQuantity. Strict mode may be disabled as well, in which case, a value with no units will fall back to the base units. When restricting the dimensions, the user must match the base dimensions exactly, without using any custom transformations.

TYPE: bool DEFAULT: False

validate

validate(v, info=None)

Validate PydanticPintQuantity.

PARAMETER DESCRIPTION
v

The quantity that should be validated.

TYPE: dict | str | Number | PlainQuantity

info

The validation info provided by the Pydantic schema.

TYPE: ValidationInfo | None DEFAULT: None

RETURNS DESCRIPTION
PlainQuantity

The validated pint.Quantity with the correct units.

RAISES DESCRIPTION
ValueError

An error occurred validating the specified value. It is raised if any of the following occur.

  • A dict is received and the keys "magnitude" and "units" do not exist.
  • There are no units provided in strict mode.
  • The units do not match in exact mode.
  • The dimensions do not match in exact mode.
  • Provided units cannot be converted to required units.
  • Provided units cannot be converted to required dimensions.
  • No such units found in registry.
  • An unknown unit was provided.
  • An unknown type for value was provided.
TypeError

An error occurred from unit registry or unit registry context. It is not propagated as a pydantic.ValidationError because it does not stem from a user error.

serialize

serialize(v, info=None, *, to_json=False)

Serialize PydanticPintQuantity.

PARAMETER DESCRIPTION
v

The quantity that should be serialized.

TYPE: PlainQuantity

info

The serialization info provided by the Pydantic schema.

TYPE: SerializationInfo | None DEFAULT: None

to_json

Whether or not to serialize to a json convertible object. Useful if using PydantiPintQuantity as a utility outside of Pydantic models.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
dict | str | Number | PlainQuantity

The serialized pint.Quantity.

get_registry

get_registry()

Get the Pydantic Pint global registry.

RETURNS DESCRIPTION
UnitRegistry

The current global registry.

set_registry

set_registry(registry)

Set the Pydantic Pint global registry.

PARAMETER DESCRIPTION
registry

The new global registry.

TYPE: UnitRegistry

PydanticPintValue

PydanticPintValue(*args, **kwargs)

Proxy class for a Pint Quantity instance with pydantic serialization.

Warning

The name PydanticPintQuantity is deprecated. Use pydantic_pint_value instead.

Unlike PydanticPintQuantity, PydanticPintValue wraps an instance of a pint quantity. Methods are added to allow it to interact with pydantic, e.g. serialization. The class immediately resolves to a pint.Quantity upon construction. The primary use for PydanticPintValue is in pydantic.Field comparison restrictions.

See pydantic_pint_value for more details.

pydantic_pint_value

pydantic_pint_value(value, units=None, /, *, ureg=None)

Construct pint.Quantity with an injected Pydantic serialization schema.

A serialization schema is added to a Pint quantity to allow it to be serialized by pydantic. This in-turn allows Pint values to be used in a pydantic.Field context.

PARAMETER DESCRIPTION
value

The magnitude of the quantity.

TYPE: Number

units

The units of the quantity. Defaults to unitless quantity.

TYPE: str | None DEFAULT: None

ureg

The unit registry from which to create the quantity. Defaults to pydantic_pint.app_registry.

TYPE: UnitRegistry | None DEFAULT: None

RETURNS DESCRIPTION
PlainQuantity

A pint.Quantity with pydantic serialization.