Repository navigation
Add quantity-kind checks, primary lookup and scoped registers - #106
Merged
Merged
Conversation
added 7 commits
November 11, 2026 16:49
Quantity accepts an optional kind: (a UnitsDB quantity id or name, or :primary). Typed operands of + and - must be the same quantity or share the UnitsDB equivalence_class, so work + heat is legal and torque + energy raises QuantityKindMismatchError although both have dimension L2MT-2. A kind whose dimension differs from the unit's raises DimensionMismatchError. Untyped quantities keep the dimension-only gating. QuantityKind.primary_for selects a unit's primary quantity from the units-schema primary flag, never from reference order, and fails explicitly when the data flags none or several. Conversion.with_register applies a validated caller register (exact factor, optional affine offset, declared dimensions) to every conversion in the block, including operand alignment in + and -. Entries that disagree with UnitsDB canonical data, declare other dimensions or carry a non-positive or inexact factor raise InvalidUnitRegisterError. Render paths that read the first quantity reference are unchanged.
A caller register makes a unit with no UnitsDB decomposition (cwt (US)) convertible, so its declared dimensions now also feed the dimension gate of + and -: 2 cwt (US) + 3 m raises DimensionMismatchError instead of succeeding through the register factor.
Resolve the scalar-division conflict in Quantity#/ by keeping main's divide_values (PR 104) and this branch's kind: propagation.
record_factor and unit_dimensions now carry the path of unit identities through si_derived_bases/root_units and raise ConversionError naming the cycle when a unit repeats. UnitsDB gram-force (NISTu196) lists kilo x itself as its root unit, so 1 gf and 1 kp to N overflowed the stack; they now fail in milliseconds. Cycle-free factors and dimensions are memoized per loaded database, unit and power.
with_register validated the new register inside a method-level ensure, so a rejected nested registration ran the ensure before 'previous' was captured and cleared the outer register. Validation now completes before the thread state changes, and the ensure restores only what this call installed.
primary_for took a unit's only reference whenever no reference was flagged true, including one explicitly marked primary: false. A sole reference is now taken only when its flag is absent (UnitsDB 2.2.0 flags multi-reference units only); an explicit false raises PrimaryQuantityError. Render selection is unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add data-declared quantity kinds, primary quantity lookup, and block-scoped caller registers. Typed arithmetic checks the dataset equivalence class, primary lookup reads the explicit flag, and register definitions validate factors, offsets and dimensions against canonical data.
Untyped callers keep their existing behavior. Mixed compatible kinds retain the left operand’s kind. The primary lookup API leaves existing render selection unchanged.
Validation: 143 focused examples pass after merging the current main, including exact division, difference-scale preservation, kind checks, register validation and Opal boot. Full/platform CI qualifies this PR head. The associated 251 reference vectors are in unitsml-testsuite PR #4. No corrected UnitsDB data pin or registry release is included.