Skip to main content

Pydantic bindings for Pint

Project description

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})

Project details


Download files

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

Source Distribution

pintdantic-0.0.3.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

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

pintdantic-0.0.3-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file pintdantic-0.0.3.tar.gz.

File metadata

  • Download URL: pintdantic-0.0.3.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.22

File hashes

Hashes for pintdantic-0.0.3.tar.gz
Algorithm Hash digest
SHA256 3dacbacae61c8b6384146b70a31281851e815cb6559a264db07ca9dbd29234a4
MD5 3a779d863240abf82917a594e124a462
BLAKE2b-256 24c3f1c2e9b50144b6d871a3c283e20cb9076250cbe50e8d467a11e4a854391d

See more details on using hashes here.

File details

Details for the file pintdantic-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: pintdantic-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.22

File hashes

Hashes for pintdantic-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3d36ca438a6644af4b8d3a3da32784bafadf5bb8fce65703be92a2ecce7934c3
MD5 da6d375b15325f0e8b7c5661b7ce892b
BLAKE2b-256 a2df18f1d00a746f35d9075caae1e8ea8cf7b9310324b888b0fbaa3931d2e373

See more details on using hashes here.

Supported by

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