Metadata-Version: 2.4
Name: pydantic-pint
Version: 0.3
Summary: Pydantic Validation for Pint Quantities.
Author-email: Tyler Hughes <tylerxh111+git@proton.me>
License: MIT License
        
        Copyright (c) 2024-2025 Tyler Hughes
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: source, https://github.com/tylerh111/pydantic-pint
Project-URL: issues, https://github.com/tylerh111/pydantic-pint/issues
Project-URL: changes, https://github.com/tylerh111/pydantic-pint/blob/main/CHANGES.md
Project-URL: documentation, https://pydantic-pint.readthedocs.io
Keywords: pydantic-pint,pydantic,pint
Classifier: Development Status :: 1 - Planning
Classifier: Framework :: Pydantic
Classifier: Framework :: Pydantic :: 2
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Topic :: Scientific/Engineering
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE.md
Requires-Dist: pint>=0.20
Requires-Dist: pydantic>=2.0
Provides-Extra: build
Requires-Dist: build; extra == "build"
Requires-Dist: bumpver; extra == "build"
Requires-Dist: setuptools>=61; extra == "build"
Requires-Dist: setuptools-scm; extra == "build"
Requires-Dist: towncrier; extra == "build"
Requires-Dist: twine; extra == "build"
Provides-Extra: dev
Requires-Dist: nox; extra == "dev"
Requires-Dist: setuptools-scm; extra == "dev"
Requires-Dist: pydantic-pint[build,docs,lint,tests]; extra == "dev"
Provides-Extra: docs
Requires-Dist: mike; extra == "docs"
Requires-Dist: mkdocs; extra == "docs"
Requires-Dist: mkdocs-exclude; extra == "docs"
Requires-Dist: mkdocs-material; extra == "docs"
Requires-Dist: mkdocs-material-extensions; extra == "docs"
Requires-Dist: mkdocs-minify-plugin; extra == "docs"
Requires-Dist: mkdocs-redirects; extra == "docs"
Requires-Dist: mkdocstrings; extra == "docs"
Requires-Dist: mkdocstrings-python; extra == "docs"
Requires-Dist: pygments; extra == "docs"
Requires-Dist: pymdown-extensions; extra == "docs"
Provides-Extra: lint
Requires-Dist: ruff; extra == "lint"
Provides-Extra: tests
Requires-Dist: pytest; extra == "tests"
Requires-Dist: pytest-cov; extra == "tests"
Dynamic: license-file

# Pydantic Pint

[![_](https://img.shields.io/pypi/v/pydantic-pint)](https://pypi.python.org/pypi/pydantic-pint)
[![_](https://img.shields.io/pypi/pyversions/pydantic-pint)](https://github.com/tylerh111/pydantic-pint)
[![_](https://img.shields.io/pypi/l/pydantic-pint)](https://github.com/tylerh111/pydantic-pint/blob/main/LICENSE.md)
[![_](https://img.shields.io/readthedocs/pydantic-pint)](https://pydantic-pint.readthedocs.io)

---

[Pydantic](https://docs.pydantic.dev) is a Python library for data validation and data serialization.
[Pint](https://pint.readthedocs.io) 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](https://pydantic-pint.readthedocs.io) aims to bridge that gap by providing Pydantic validation for Pint quantities.

```python
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`](https://pypi.python.org/pypi/pydantic-pint) on PyPI.

Pydantic Pint requires both Pydantic and Pint to be installed.
It also requires [`typing.Annotated`](https://docs.python.org/3/library/typing.html#typing.Annotated) (for older version of python use [`typing-extensions`](https://pypi.org/project/typing-extensions/)).

```shell
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.

```python
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.

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