Skip to content

units

adh.geom.units ¤

Define Pydantic ping.Quantity.

Derived works from pydantic-pint with modifications:

  • Support validation of a list

pydantic-pint 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.

Classes:

  • PintAnno

    Pydantic Pint Annotation.

Functions:

PintAnno ¤

PintAnno(_arg: PintQty, /, *, ureg: Optional[UnitRegistry] = None)

Pydantic Pint Annotation.

Parameters:

  • _arg (PintQ) –

    Either the units or dimensions to check the Pydantic field. For example, "sec", "[length]", or {"[length]": 1, "[time]": -2}

  • ureg (Optional[UnitRegistry], default: None ) –

    A custom Pint unit registry

Methods:

Source code in src/adh/geom/units.py
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def __init__(
    self,
    _arg: PintQty,
    /,
    *,
    ureg: Optional[pint.UnitRegistry] = None,
) -> None:
    """Initialise units and dimensions.

    Args:
        _arg (PintQ):
            Either the units or dimensions to check the Pydantic field.
            For example, `"sec"`, `"[length]"`, or `{"[length]": 1, "[time]": -2}`
        ureg (Optional[pint.UnitRegistry]):
            A custom Pint unit registry
    """
    self.ureg = ureg if ureg else _DEFAULT_UNIT_REGISTRY
    self.dimensions = self.ureg.get_dimensionality(_arg)

__get_pydantic_core_schema__ ¤

__get_pydantic_core_schema__(source_type: Any, handler: GetCoreSchemaHandler) -> CoreSchema

Gets the Pydantic core schema.

Parameters:

Returns:

  • CoreSchema

    The Pydantic core schema.

Source code in src/adh/geom/units.py
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
def __get_pydantic_core_schema__(
    self,
    source_type: Any,
    handler: GetCoreSchemaHandler,
) -> core_schema.CoreSchema:
    """Gets the Pydantic core schema.

    Args:
        source_type:
            The source type.
        handler:
            The `GetCoreSchemaHandler` instance.

    Returns:
        The Pydantic core schema.
    """
    _from_typedict_schema = {
        "magnitude": core_schema.typed_dict_field(
            core_schema.str_schema(coerce_numbers_to_str=True),
        ),
        "units": core_schema.typed_dict_field(
            core_schema.str_schema(),
            required=False,
        ),
    }

    validate_schema = core_schema.chain_schema(
        [
            core_schema.union_schema(
                [
                    core_schema.is_instance_schema(Quantity),
                    core_schema.str_schema(coerce_numbers_to_str=True),
                    core_schema.typed_dict_schema(_from_typedict_schema),
                ]
            ),
            core_schema.with_info_plain_validator_function(self.validate),
        ]
    )

    validate_json_schema = core_schema.chain_schema(
        [
            core_schema.union_schema(
                [
                    core_schema.str_schema(coerce_numbers_to_str=True),
                    core_schema.typed_dict_schema(_from_typedict_schema),
                ]
            ),
            core_schema.no_info_plain_validator_function(self.validate),
        ]
    )

    serialize_schema = core_schema.plain_serializer_function_ser_schema(
        self.serialize,
        info_arg=True,
    )

    return core_schema.json_or_python_schema(
        json_schema=validate_json_schema,
        python_schema=validate_schema,
        serialization=serialize_schema,
    )

serialize ¤

serialize(v: PlainQuantity, info: SerializationInfo | None = None) -> dict

Serialise a Pydantic Pint Quantity.

Source code in src/adh/geom/units.py
129
130
131
132
133
134
135
136
137
138
def serialize(
    self,
    v: Quantity,
    info: core_schema.SerializationInfo | None = None,
) -> dict:
    """Serialise a Pydantic Pint Quantity."""
    return {
        "magnitude": v.magnitude,
        "units": f"{v.units}",
    }

validate ¤

validate(v: dict | str | Number | PlainQuantity, info: ValidationInfo | None = None) -> PlainQuantity

Validate a Pydantic Pint Quantity.

Source code in src/adh/geom/units.py
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
def validate(
    self,
    v: dict | str | Number | Quantity,
    info: core_schema.ValidationInfo | None = None,
) -> Quantity:
    """Validate a Pydantic Pint Quantity."""
    # Given a dict of {'magnitude': m, 'units': u} convert to a string for ureg()
    if isinstance(v, dict):
        try:
            # TODO: handle a list of magnitudes
            v = f"{v['magnitude']} {v.get('units', '')}"
        except KeyError as e:
            raise ValueError("no `magnitude` or `units` keys found.") from e

    # Either a dict converted to str or called on a string
    if isinstance(v, str):
        try:
            v = self.ureg(v)
        except pint.UndefinedUnitError as e:
            raise ValueError(e) from e

    # TODO: handle if v is a list

    try:
        return self._validate_dimensions(v)
    except pint.DimensionalityError as e:
        raise ValueError(e) from e
    except KeyError as e:
        # raise TypeError instead of KeyError
        raise TypeError(f"unknown unit registry context {e}.") from e
    except Exception as e:
        raise ValueError(f"unknown error: {v=} | {type(v)=}.") from e

get_registry ¤

get_registry() -> UnitRegistry

Get the shared Pint Unit Registry.

Source code in src/adh/geom/units.py
55
56
57
def get_registry() -> pint.UnitRegistry:
    """Get the shared Pint Unit Registry."""
    return _DEFAULT_UNIT_REGISTRY