Repository navigation
units: build PhysicalQuantity with a factory as a frozen result, and split quantity.py into modules - #4
Merged
Conversation
…lone functions, make it immutable and picklable, update tests, README, and changelog.
…y`, and `_split.py` for SI unit conversions, LaTeX rendering, and parse tree splitting. Update `quantity.py` to delegate responsibilities to the new modules.
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.
Stacked on #3: merge that first, and this PR's base will then switch to
main.PhysicalQuantitybecomes an immutable result built by a factory, and the code that reads a quantity is split out ofquantity.py. Parsing results are unchanged.Changes
PhysicalQuantity.from_tree(name, params, ast_root, parser)runs each step as a separate function: split value from unit, restore the value's text, LaTeX, then the SymPy forms in SI base units.parse_quantitycalls it.PhysicalQuantityis a@dataclass(frozen=True, eq=False)with the same attributes andrepr. Quantities still compare by identity.self.ast_root.__getstate__/__setstate__are gone. Pickle and deepcopy just work, and an existing pickle still loads.FrozenInstanceError.reverted_unitsis a tuple.from_tree, orparse_quantityas before.quantity.parseris deprecated (removed in 1.0). It warns and returns the same cached parser asbuild_quantity_parser(params.unit_sets, params.strictness).quantity.pykeeps the publicPhysicalQuantity,RevertedUnit,parse_quantityandsame_dimension. The steps move to private modules:_split.py: the rotations,split_value_and_unit, the unit checks andMAX_UNIT_EXPONENT._latex.py:latex_for_unitandlatex_for_value._conversion.py:quantity_parsing_configandsympy_forms.unsplittable_names(unit_sets, all_forms)moves todata.py, so the new modules don't import each other.lambdafeedback.units. Onlylambdafeedback.units.quantity.MAX_UNIT_EXPONENTno longer resolves; nothing here or in compareExpressions uses it.Downstream
compareExpressions only reads
value,unit,unit_latexandlatex, and loops overreverted_units, so it isn't affected.Verification
parser.tools/parity/check.py: 0 unexpected changes.repr, LaTeX, SymPy forms,reverted_units) over the parity corpus plus extra juxtaposition inputs, in both strict and natural mode, is byte-identical before and after both commits.scripts/check_dist.pypasses.🤖 Generated with Claude Code