Skip to main content

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.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantic-pint 0.6.0
File Size Uploaded
pydantic_pint-0.6.0.tar.gz 190.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-pint 0.6.0
File Interpreter ABI Platform
pydantic_pint-0.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 201.0 kB

Release files / pydantic_pint-0.6.0.tar.gz

Download URL pydantic_pint-0.6.0.tar.gz
Size 190.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c131a39fefb337799fde2e41896696cb92702b222f0a86a520c477d739623fe1
BLAKE2b-256 checksum
How to use checksums
13d90a20c61c9e171243927542f22816d44b420d9459243f96825ebb9e3206bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.11

Release files / pydantic_pint-0.6.0-py3-none-any.whl

Download URL pydantic_pint-0.6.0-py3-none-any.whl
Size 10.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
15157d30502d7293a002f116d19f5ac6889dc3023855560deb3e1245fe1bbe72
BLAKE2b-256 checksum
How to use checksums
370e0c3e467d20ae48454955aeab519e349e86de99b1fc5855d9e63dcd187ea6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.11

Release history Release notifications | RSS feed

0.7.0

2 release files

This release

0.6.0 This release

2 release files

0.5.0

2 release files

0.4

2 release files

0.3

2 release files

0.2

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page