Quantity
pydantic_pint.quantity ¶
Defines the Pydantic pint.Quantity.
PydanticPintQuantity ¶
PydanticPintQuantity(_arg, /, *, ureg=None, ureg_contexts=None, restriction=None, ser_mode=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 dimension, then any unit of that dimension is allowed. |
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: |
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: |
Source code in src/pydantic_pint/quantity.py
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 |
Source code in src/pydantic_pint/quantity.py
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 | PlainQuantity | The serialized |
Source code in src/pydantic_pint/quantity.py
__get_pydantic_core_schema__ ¶
Gets the Pydantic core schema.
| PARAMETER | DESCRIPTION |
|---|---|
source_type | The source type. TYPE: |
handler | The TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
CoreSchema | The Pydantic core schema. |