Repository navigation
refactor: redesign unit display with structured terms - #146
Merged
Merged
Conversation
The template had a stale header/imports and the generator dropped the prefixed kelvin exports and duplicated liter/litre in __all__, so regenerating silently changed the module. It now reproduces saiunit/_unit_common.py byte for byte.
Units now carry canonical (atom, exponent) display terms instead of string triples re-parsed by regex. mul/div/pow/reverse share one composition function; dim/scale/factor are derived in canonical order. - Composites display as written (mV * ms -> "ms * mV"); auto-collapse is limited to SI base units, N, Pa, J, W, C, V, F, ohm, S, Hz and their prefixed forms. New Unit.simplify()/Quantity.simplify() collapse onto any registered unit. - Anonymous operands keep names (UNITLESS / mV -> "1 / mV"); s**-1 -> Hz; readable scaled dimensionless units; exact rational exponents. - New tokenizer/parser: parse_unit(str(u)) == u, idempotent, accepts µ/μ, Ω, °, superscripts, rational exponents, km/h-style symbols. - Unit.to_string() with ascii/unicode/latex styles, __format__, _repr_latex_; Quantity prints "0.4 mV^-1" for numerator-less units. - Canonical ASCII symbols (um, deg, arcmin, arcsec, Angstrom); mmHg is its own unit (133.322387415 Pa), no longer an alias of torr. - Legacy pickles still load; generator emits auto_simplify flags and create_power for powered units.
Contributor
|
Sorry @chaoming0625, your pull request is larger than the review limit of 150,000 diff characters |
3 tasks done
chaoming0625
added a commit
that referenced
this pull request
Oct 21, 2026
…ctness pass (#147) * release: v0.6.0 — unit display redesign, JAX 0.12 support, core correctness pass - Redesign unit composition, display and parsing around structured terms: composites display as written, curated auto-simplification plus Unit.simplify()/Quantity.simplify(), exact rational exponents, round-trip parsing, Unicode/LaTeX rendering (#146). - JAX 0.12 compatibility: NumPy 2 __array__(copy=), guarded private imports, public custom_jvp and sparse APIs (#145). - Core Quantity/Unit/Dimension correctness fixes (#140, #145) and measured CuPy backend support (#141); docs taxonomy and backend outputs (#142). - Require Python >= 3.12 and jax >= 0.9.0; mmHg is no longer an alias of torr. * docs(changelog): limit round-trip claim to ASCII/Unicode forms
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.
Summary
Units now carry canonical
(atom, exponent)display terms instead of(name, dispname, exp)string triples re-parsed by regex.*,/,**andreverse()share one composition function, anddim/scale/factorare derived from the terms in canonical order. New modules:_unit_terms.py,_unit_format.py,_unit_parse.py.UNITLESS / mV10.0^3 * s^3 * A / (m^2 * kg)1 / mVmV * ms,mol / l,km * mmuWb,M,m^2ms * mV,mol / l,km * mmrad * mmm * rads ** -11 / sHzmV / V,minute * Hz10.0^-3,6.0 * 10.0^1mV / V,Hz * minmV ** (1/3)mV^0.3333333333333333mV^(1/3)Quantity(0.4, 1/mV)0.4 1 / mV0.4 mV^-1micron,degreeµm,°um,degmmHgtorrUnit.simplify()/Quantity.simplify()collapse onto any registered unit ((V * s).simplify()→Wb).parse_unit(str(u)) == uand parsing is idempotent. The parser acceptsµ/μ,Ω,°, superscripts,·/×, rational exponents (m^(1/3)) and registered symbols such askm/h.Unit.to_string()supports ascii/unicode/latex styles (and their inline variants), plus__format__and_repr_latex_forUnitandQuantity.a * bandb * adisplay identically and compare exactly equal, andhour.reverse().reverse() == hour.Test plan
pytest saiunit/on JAX 0.12.0: 4178 passed. On JAX 0.11.2: 3908 passed.mypy saiunit/clean on both JAX versions; no-JAX tests passdev/bench_unit_display.py): composition,strand import time are at parity with main