Skip to main content

pytest codecov

pintdantic

Pydantic bindings for Pint

Installation

pip install pintdantic

Quick Start

from pintdantic import QuantityModel, QuantityField
from pint import Quantity

class MyModel(QuantityModel):
    length: QuantityField
    width: QuantityField

# Create a model with Quantity objects
model = MyModel(
    length=Quantity(10.0, "m"),
    width=Quantity(5.0, "m")
)

# Serialize to dict (condensed format by default)
data = model.to_dict()
# {"length": [10.0, "meter"], "width": [5.0, "meter"]}

# Load from dict
loaded_model = MyModel.from_dict(data)

Serialization Formats

Pintdantic supports two serialization formats for Quantity values:

Condensed Format (Default)

By default, Quantity values are serialized to a compact array format:

model = MyModel(length=Quantity(100.0, "watt"))
data = model.to_dict()
# {"length": [100.0, "watt"]}

This format is more compact and produces smaller JSON payloads.

Verbose Format

You can use the verbose format (dict-based) by setting verbose=True:

# Serialize with verbose format
data = model.to_dict(verbose=True)
# {"length": {"magnitude": 100.0, "units": "watt"}}

# Save to file with verbose format
model.save("output.json", verbose=True)

# Using Pydantic's model_dump with context
data = model.model_dump(context={'verbose': True})

Backwards Compatibility

Pintdantic can load data in either format, providing full backwards compatibility:

# Both formats work for loading
condensed_data = {"length": [10.0, "meter"], "width": [5.0, "meter"]}
verbose_data = {
    "length": {"magnitude": 10.0, "units": "meter"},
    "width": {"magnitude": 5.0, "units": "meter"}
}

model1 = MyModel.from_dict(condensed_data)  # Works!
model2 = MyModel.from_dict(verbose_data)    # Also works!

Input Formats

Pintdantic accepts multiple input formats for Quantity fields:

# Quantity objects
MyModel(length=Quantity(10.0, "m"))

# Tuples (magnitude, units)
MyModel(length=(10.0, "m"))

# Lists [magnitude, units] - JSON-compatible
MyModel(length=[10.0, "m"])

# Dicts (verbose format)
MyModel(length={"magnitude": 10.0, "units": "m"})

# Bare numbers (if defaults are set)
class ModelWithDefaults(QuantityModel):
    length: QuantityField = (10.0, "m")

ModelWithDefaults(length=5.0)  # Uses default units "m"

Nested Models

The verbose flag works with nested models and propagates through the entire serialization tree:

class Inner(QuantityModel):
    measurement: QuantityField

class Outer(QuantityModel):
    name: str
    inner: Inner
    height: QuantityField

outer = Outer(
    name="test",
    inner=Inner(measurement=Quantity(5.0, "cm")),
    height=Quantity(10.0, "m")
)

# Condensed (default)
data = outer.to_dict()
# {
#     "name": "test",
#     "inner": {"measurement": [5.0, "centimeter"]},
#     "height": [10.0, "meter"]
# }

# Verbose
data = outer.to_dict(verbose=True)
# {
#     "name": "test",
#     "inner": {"measurement": {"magnitude": 5.0, "units": "centimeter"}},
#     "height": {"magnitude": 10.0, "units": "meter"}
# }

Usage with Pydantic BaseModel

When using QuantityModel within a regular Pydantic BaseModel, you can control the serialization format using Pydantic's context:

from pydantic import BaseModel

class RegularModel(BaseModel):
    id: int
    quantities: MyQuantityModel

model = RegularModel(id=1, quantities=MyQuantityModel(...))

# Condensed format (default)
data = model.model_dump()

# Verbose format using context
data = model.model_dump(context={'verbose': True})

Metadata

Release files for pintdantic 0.0.4

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

Source distribution (sdist)

Source distribution for pintdantic 0.0.4
File Size Uploaded
pintdantic-0.0.4.tar.gz 8.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pintdantic 0.0.4
File Interpreter ABI Platform
pintdantic-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 17.0 kB

Release files / pintdantic-0.0.4.tar.gz

Download URL pintdantic-0.0.4.tar.gz
Size 8.6 kB
Tags Source
SHA-256 checksum
How to use checksums
21e56c73a06c0727010aef486e2eb5261c389d9977592f9aee3842c5371a4525
BLAKE2b-256 checksum
How to use checksums
46efa658620002b73534ebdfc22169fb0011f744bfa89b737c54367165eca263
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.22

Release files / pintdantic-0.0.4-py3-none-any.whl

Download URL pintdantic-0.0.4-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4ea6b8bb52a61bdd6cdb0124ad411ebd40c6844794958ff6e740f16711ea58b5
BLAKE2b-256 checksum
How to use checksums
af58e3a3e3fa4234b5779dd9ea17e71a218527fdd577a3d3b709ff3d40d85be9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.22

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

0.0.0

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