Skip to main content

Pydantic validation for Pint Quantities.

Project description

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 Annotations

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

Project details


Release history Release notifications | RSS feed

This version

0.1

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.1.tar.gz (19.7 kB view details)

Uploaded Source

Built Distribution

pydantic_pint-0.1-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydantic_pint-0.1.tar.gz
  • Upload date:
  • Size: 19.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for pydantic_pint-0.1.tar.gz
Algorithm Hash digest
SHA256 488a256e37724970dbeb736d38a45bc3a9da4112a59edb094d9f87428a2b5467
MD5 c06582001e7bb86735da33ad8880028c
BLAKE2b-256 5fbcc82c85f1be1fa58743281205511323b39b65ff8c1f5a4ead60a31c358136

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pydantic_pint-0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for pydantic_pint-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4d04125df91f080facea67c5bede068ba5f7f8453bf69c3c3ae18ca901a84e76
MD5 b2f8e817f0d8ab26d4525ead0e748b65
BLAKE2b-256 00880ca0ce9d10a8cf2983d24483572cdd4e7ab3ec7d0a2d0f0ae9e3d4a28a74

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page