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"}'

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_pint-0.5.0.tar.gz (189.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_pint-0.5.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_pint-0.5.0.tar.gz.

File metadata

  • Download URL: pydantic_pint-0.5.0.tar.gz
  • Upload date:
  • Size: 189.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for pydantic_pint-0.5.0.tar.gz
Algorithm Hash digest
SHA256 b12b1fa79856a9cd90a7a62a9c8d2df427c4c2f341601dcc170c5b4dac8ecda0
MD5 6d824b837dd59cb363021376a73a81e3
BLAKE2b-256 93b486b4b31e9606feb0cd903cb3ecbef8a14ab37b04ffa70669dbb716d64bbf

See more details on using hashes here.

File details

Details for the file pydantic_pint-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: pydantic_pint-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for pydantic_pint-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6d1cc3f3a90373300f0a72890d883a0124f42274636eabd59e3d0a4b020d493a
MD5 285ffa2bc90f9fb11916acf33e7f61bc
BLAKE2b-256 8abb1e8345890b8430894e614627667ab87ed277d591fdcc8c475ec7e7f0c8e1

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 files

0.4

2 files

0.3

2 files

0.2

2 files

0.1

2 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