test(precision): characterize and guard the Decimal->float JSON boundary#205
Closed
robcohen wants to merge 1 commit into
Closed
test(precision): characterize and guard the Decimal->float JSON boundary#205robcohen wants to merge 1 commit into
robcohen wants to merge 1 commit into
Conversation
48868ed to
7eba21d
Compare
a343b17 to
bb9790c
Compare
The JSON API serializes Decimals via float() (core/charts.py), so exact ledger values cross the wire as float64. The frontend renders at display precision, so typical currency values are unaffected in the UI — but exports, raw API consumers, and high-precision values see the rounding. Layer 2 of the correctness plan: pin the boundary so it can't regress and a fix is measurable. - test_typical_values_are_lossless: currency + 8dp crypto + bounded magnitudes round-trip exactly (guards the common case). - test_high_precision_is_lost: xfail(strict) documenting that values beyond ~16 significant digits are rounded on the wire; flips green when numbers are emitted exactly (needs an exact JSON encoder + frontend decimal support, which also regenerates all snapshots — that epic, not an isolated wire change). - test_api_numbers_are_json_numbers_not_strings: pins the current wire contract so any format switch is deliberate.
7eba21d to
6fa8318
Compare
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.
Layer 2 of the correctness plan — pins the Decimal→float64 JSON boundary (C2 in the analysis).
What I found investigating C2
Decimalviafloat()(core/charts.py:_json_default). But the frontend has no decimal type (Amount.numberis a JSnumber) and renders throughIntl.NumberFormatat display precision — so typical currency values are not visibly wrong in the rendered UI. C2 is therefore a data-fidelity defect (CSV/Excel export viautil/excel.py, raw JSON API consumers, column sorting, and high-precision values), not a wrong-number-in-the-report defect.620/7, very large magnitudes, e.g.9999999999999999.01 → 1e16).jsoncan't emit exact decimal literals (its C encoder ignores afloatsubclass's__repr__), so it needs a new dependency (simplejson/use_decimal), it regenerates all 53 snapshots, and it only reaches the user once the frontend gains decimal support. That's an epic to scope deliberately, not an isolated PR.What this PR does
Adds
tests/test_number_precision_boundary.pyto make the boundary explicit and regression-proof:test_typical_values_are_lossless— currency/crypto/bounded magnitudes must cross the wire exactly (guards the common case).test_high_precision_is_lost—xfail(strict)documenting the loss beyond ~16 sig digits; flips green automatically when numbers are emitted exactly, so it's the acceptance test for the eventual fix.test_api_numbers_are_json_numbers_not_strings— pins the current wire contract so a format switch can't happen silently.9 passed, 3 xfailed; ruff + mypy clean.
Recommendation
Given the UI is protected by display rounding, I'd prioritize the genuinely-wrong-number risks next (silent mixed-currency conversion totals, and cost-lot identity/ghost lots) over the exact-wire epic. This PR locks C2's boundary in the meantime.
Stacked on #202 (base branch) for green CI while
mainis red from the auto-merged bump PRs; retarget tomainafter #202 merges.🤖 Generated with Claude Code