Skip to content

ucon.checking

Runtime dimension enforcement via decorator.

ucon.checking

Runtime dimensional validation for functions accepting Number arguments.

Provides the @enforce_dimensions decorator, which validates Number arguments against their Number[Dimension.X] annotations at call time.

enforce_dimensions(fn)

Validate Number arguments against their Number[Dimension] annotations.

Only parameters annotated as Number[Dimension.X] are checked. Plain Number parameters and non-Number parameters are ignored.

Checks are precomputed at decoration time. Per-call overhead is one dict lookup and one dimension comparison per constrained parameter.

Parameters

fn : callable The function to wrap.

Returns

callable Wrapped function with dimensional validation on entry.

Raises

TypeError If a constrained argument is not a Number instance. ValueError If a Number's dimension does not match the annotated constraint.

Example

@enforce_dimensions ... def speed(distance: Number[Dimension.length], time: Number[Dimension.time]) -> Number: ... return distance / time