Pydantic Pint
Pydantic is a Python library for data validation and data serialization. Pint is a Python library for defining, operating, and manipulating physical quantities. By default, they do not play well with each other.
Many projects that have a need for data validation may also need to work with physical quantities. Pydantic Pint aims to bridge that gap by providing Pydantic validation for Pint quantities.
from pydantic import BaseModel
from pydantic_pint import PydanticPintQuantity
from pint import Quantity
from typing import Annotated
class Box(BaseModel):
length: Annotated[Quantity, PydanticPintQuantity("m")]
width: Annotated[Quantity, PydanticPintQuantity("m")]
box = Box(
length="4m",
width="2m",
)
Getting Started
Installation
Pydantic Pint is available as pydantic-pint on PyPI.
Pydantic Pint requires both Pydantic and Pint to be installed.
It also requires typing.Annotated (for older version of python use typing-extensions).
pip install pydantic-pint
Usage
Pydantic Pint provides PydanticPintQuantity which enabled Pydantic validation for Pint quantities.
For a field of a Pydantic model to have quantity validation, it must be annotated with a PydanticPintQuantity for a given unit.
from pydantic import BaseModel
from pydantic_pint import PydanticPintQuantity
from pint import Quantity
from typing import Annotated
class Coordinates(BaseModel):
latitude: Annotated[Quantity, PydanticPintQuantity("deg")]
longitude: Annotated[Quantity, PydanticPintQuantity("deg")]
altitude: Annotated[Quantity, PydanticPintQuantity("km")]
Users of the model can input anything to the field with a specified unit that is convertible to the units declared in the annotation.
For instance, the units for Coordinates.altitude are kilometers, however users can specify meters instead.
PydanticPintQuantity will handle the conversion from meters to kilometers.
coord = Coordinates(
latitude="39.905705 deg",
longitude="-75.166519 deg",
altitude="12 meters",
)
print(coord)
#> latitude=<Quantity(39.905705, 'degree')> longitude=<Quantity(-75.166519, 'degree')> altitude=<Quantity(0.012, 'kilometer')>
print(f"{coord!r}")
#> Coordinates(latitude=<Quantity(39.905705, 'degree')>, longitude=<Quantity(-75.166519, 'degree')>, altitude=<Quantity(0.012, 'kilometer')>)
print(coord.model_dump())
#> {'latitude': <Quantity(39.905705, 'degree')>, 'longitude': <Quantity(-75.166519, 'degree')>, 'altitude': <Quantity(0.012, 'kilometer')>}
print(coord.model_dump(mode="json"))
#> {'latitude': '39.905705 degree', 'longitude': '-75.166519 degree', 'altitude': '0.012 kilometer'}
print(f"{coord.model_dump_json()!r}")
#> '{"latitude":"39.905705 degree","longitude":"-75.166519 degree","altitude":"0.012 kilometer"}'
Metadata
Release files for pydantic-pint 0.7.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pydantic_pint-0.7.0.tar.gz | 252.2 kB | Without provenance | Details for pydantic_pint-0.7.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pydantic_pint-0.7.0-py3-none-any.whl | Python 3 | none | any | Details for pydantic_pint-0.7.0-py3-none-any.whl |
Total release size: 262.8 kB
Release files / pydantic_pint-0.7.0.tar.gz
| Download URL | pydantic_pint-0.7.0.tar.gz |
|---|---|
| Size | 252.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3eee0678eede1c5c80de740392999b1f4cce1f48a09c70305cc969d40dd0d579
|
|
BLAKE2b-256 checksum How to use checksums |
db6fdd9dcaaad95feb45f56c78847f1427460da6d6c266334992cbbd3948d698
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.11
|
Release files / pydantic_pint-0.7.0-py3-none-any.whl
| Download URL | pydantic_pint-0.7.0-py3-none-any.whl |
|---|---|
| Size | 10.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
398361eddded97c73d42903e5d312d6e9cfc575c6090c3d983ee3f9af3716134
|
|
BLAKE2b-256 checksum How to use checksums |
3247d601e9c9df260d36f9064ff5d4aac27fe40917b850c4ab4a875b707550ff
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.11
|
Security for pydantic-pint 0.7.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.