nexusLIMS.schemas.pint_types#

Pydantic integration for Pint Quantity objects.

This module provides custom Pydantic types and validators for handling Pint Quantity objects in Pydantic models. It enables seamless validation and serialization of physical quantities with units in NexusLIMS metadata schemas.

The custom types support:

  • Validation of Quantity objects, strings, and numeric values

  • Automatic conversion to preferred units

  • JSON serialization for API/storage

  • Type hints for IDE support

Examples:

Use in a Pydantic model:

>>> from pydantic import BaseModel
>>> from nexusLIMS.schemas.pint_types import PintQuantity
>>> from nexusLIMS.schemas.units import ureg
>>>
>>> class MyMetadata(BaseModel):
...     voltage: PintQuantity
...     current: PintQuantity | None = None
>>>
>>> # Create from Quantity
>>> meta = MyMetadata(voltage=ureg.Quantity(10, "kV"))
>>> print(meta.voltage)
10 kilovolt
>>>
>>> # Create from string
>>> meta = MyMetadata(voltage="10 kV")
>>> print(meta.voltage)
10 kilovolt
>>>
>>> # Serialize to JSON
>>> meta.model_dump()
{'voltage': {'value': 10.0, 'units': 'kilovolt'}, 'current': None}

Module Contents#

Data#

PintQuantity

Type alias for Pint Quantity fields in Pydantic models.

API#

nexusLIMS.schemas.pint_types.PintQuantity#

Type alias for Pint Quantity fields in Pydantic models.

Use this type hint for fields that should accept and validate physical quantities with units. The field will accept:

  • Pint Quantity objects

  • String representations like “10 kV” or “5.2 mm”

  • Dicts with ‘value’ and ‘units’ keys (from JSON deserialization)

  • Numeric values (interpreted as dimensionless)

  • None (if field is optional)

The field will serialize to JSON as a dict with ‘value’ and ‘units’ keys.

Examples:

Define a model with Quantity fields:

>>> from pydantic import BaseModel
>>> from nexusLIMS.schemas.pint_types import PintQuantity
>>> from nexusLIMS.schemas.units import ureg
>>>
>>> class ImageMetadata(BaseModel):
...     voltage: PintQuantity
...     working_distance: PintQuantity | None = None
...
>>> # Create from Quantity objects
>>> meta = ImageMetadata(
...     voltage=ureg.Quantity(10, "kilovolt"),
...     working_distance=ureg.Quantity(5.2, "millimeter"),
... )
>>>
>>> # Create from strings
>>> meta = ImageMetadata(voltage="10 kV", working_distance="5.2 mm")
>>>
>>> # Serialize to JSON
>>> import json
>>> print(json.dumps(meta.model_dump(), indent=2))
{
  "voltage": {
    "value": 10.0,
    "units": "kilovolt"
  },
  "working_distance": {
    "value": 5.2,
    "units": "millimeter"
  }
}
>>>
>>> # Deserialize from JSON
>>> json_data = '''
... {
...   "voltage": {"value": 15.0, "units": "kilovolt"},
...   "working_distance": {"value": 10.0, "units": "millimeter"}
... }
... '''
>>> meta = ImageMetadata.model_validate_json(json_data)
>>> print(meta.voltage)
15.0 kilovolt

See Also: nexusLIMS.schemas.units Pint unit registry and utilities

nexusLIMS.schemas.metadata Metadata schemas using PintQuantity fields