saiunit.typing.validate_units#
- saiunit.typing.validate_units(func=None, *, strict=False)[source]#
Decorator that validates
Quantityargument units at call time.Inspects the function’s type annotations for
Quantity[...]types (produced byQuantity[u.meter]orQuantity["length"]) and checks every annotated argument on each call.- Parameters:
func (callable, optional) – The function to decorate. If
None, returns a partial decorator so that@validate_units(strict=True)works.strict (
bool) – IfTrue, require exact unit match (same scale). IfFalse(default), only require dimensional compatibility.
- Returns:
The decorated function with unit validation.
- Return type:
callable
- Raises:
saiunit.UnitMismatchError – If an argument’s unit/dimension does not match the annotation.
TypeError – If an annotated parameter is not a
Quantity.
Examples
>>> import saiunit as u >>> from saiunit.typing import validate_units >>> >>> @validate_units ... def ohms_law(V: u.Quantity[u.volt], R: u.Quantity[u.ohm]) -> u.Quantity[u.amp]: ... return V / R ... >>> ohms_law(5.0 * u.volt, 100.0 * u.ohm) Quantity(0.05, "A")