Use approximate float comparison for JSON test records#200
Merged
JerBouma merged 2 commits intoJerBouma:mainfrom Mar 13, 2026
Merged
Use approximate float comparison for JSON test records#200JerBouma merged 2 commits intoJerBouma:mainfrom
JerBouma merged 2 commits intoJerBouma:mainfrom
Conversation
Python's shortest-repr float formatting varies across versions, causing recorder mismatches on values like 0.011710000000000002 vs 0.01171. For JSON record files, fall back to math.isclose when the exact strings differ, using a relative tolerance of 1e-9. Fixes JerBouma#196
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.
Fixes #196.
Python's shortest-repr float formatting varies across versions (and platforms), which causes the test recorder to detect spurious changes in JSON fixture files. For example, a value recorded as
0.011710000000000002may serialize as0.01171in a newer environment -- both represent the same IEEE 754 double, just with different string representations.What changed:
In
tests/conftest.py, theRecordclass now falls back tomath.isclose(relative tolerance 1e-9) when comparing JSON record files whose string representations differ. The exact-match fast path is tried first, so there is zero overhead when strings already match. Non-JSON records (CSV, txt) are unaffected.How it works:
_values_approx_equalrecursively walks two JSON-parsed structures, usingmath.isclosefor float-to-float pairs and strict equality for everything else (ints, strings, bools, nulls, structure/length)._json_strings_approx_equalwraps the above withjson.loadsand catches decode errors (falling back to exact comparison).record_changedchecks the file extension and only uses the approximate path for.jsonrecords.No recorded fixtures were changed. No new dependencies.