Documentation¶
pydantic_pint
¶
Pydantic validation for Pint Quantities.
app_registry
module-attribute
¶
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 |
ureg
|
A custom Pint unit registry.
If not specified, the default unit registry from
TYPE:
|
ureg_contexts
|
A custom Pint context (or context name) for the default unit registry. All contexts are applied in validation conversion. |
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:
|
ser_mode
|
The mode for serializing the field; either
TYPE:
|
ser_mode_json
|
The mode for serializing the field specifically for Pydantic's
TYPE:
|
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
TYPE:
|
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
TYPE:
|
validate
¶
Validate PydanticPintQuantity.
| PARAMETER | DESCRIPTION |
|---|---|
v
|
The quantity that should be validated.
TYPE:
|
info
|
The validation info provided by the Pydantic schema.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
PlainQuantity
|
The validated |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
An error occurred validating the specified value. It is raised if any of the following occur.
|
TypeError
|
An error occurred from unit registry or unit registry context.
It is not propagated as a |
serialize
¶
Serialize PydanticPintQuantity.
| PARAMETER | DESCRIPTION |
|---|---|
v
|
The quantity that should be serialized.
TYPE:
|
info
|
The serialization info provided by the Pydantic schema.
TYPE:
|
to_json
|
Whether or not to serialize to a json convertible object.
Useful if using
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict | str | Number | PlainQuantity
|
The serialized |
get_registry
¶
Get the Pydantic Pint global registry.
| RETURNS | DESCRIPTION |
|---|---|
UnitRegistry
|
The current global registry. |
set_registry
¶
Set the Pydantic Pint global registry.
| PARAMETER | DESCRIPTION |
|---|---|
registry
|
The new global registry.
TYPE:
|
PydanticPintValue
¶
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
¶
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:
|
units
|
The units of the quantity. Defaults to unitless quantity.
TYPE:
|
ureg
|
The unit registry from which to create the quantity.
Defaults to
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
PlainQuantity
|
A |