Don't collapse a plain decimal literal like 1.0 to an int - #43
Open
afonsojanu wants to merge 1 commit into
Open
afonsojanu wants to merge 1 commit into
afonsojanu wants to merge 1 commit into
Conversation
The number parser in decoder.py used to check whether a parsed float was numerically whole and, if so, cast it to int regardless of how the value was written. That means "1.0" and "10.00" came back as plain ints, even though hjson's own serializer round-trips a float 1.0 back to "1.0", and the plain json module keeps it a float too. The collapsing logic is genuinely useful for a case like "17.01e2", where the exponent (not the decimal point) is what pushes the value to a whole number, and existing fixtures already rely on that giving an int. This change narrows the collapse so it only fires when an exponent is present, leaving a bare decimal literal such as "1.0" as a float. Updated the comments_test fixture's expected output to match (its num2 field is 0.0, not 0) and added a couple of decode tests pinning both the fixed and preserved behavior.
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 #24.
hjson.loads('{test: 1.0}')currently hands back anint, not afloat:That's inconsistent both with the standard
jsonmodule (which keeps1.0a float) and with hjson's own serializer, sincehjson.dumps(1.0)already produces"1.0". So a value written with an explicit decimal point doesn't round-trip throughloads/dumps.The cause is in
scantfnnsinhjson/decoder.py. After parsing the number as a float, it checks whether the result is numerically whole and, if so, casts it down toint:This collapse is intentional for a case like
17.01e2, where the exponent pushes an otherwise-fractional value up to a whole number (1701.0->1701), and the existing test fixtures (kan_test.hjson/kan_result.json) already pin that behavior. But the same check also fires for a plain decimal literal such as1.0or10.00, where there's no exponent at all - the decimal point was written on purpose and shouldn't be discarded.The fix narrows the collapse to only apply when an exponent is present, so it keeps the
17.01e2 -> 1701behavior but leaves1.0and10.00as floats.One existing fixture (
comments_test.hjson) had anum2: 0.0entry whose expected result (comments_result.json/comments_result.hjson) was pinning the old, buggy behavior ("num2": 0instead of0.0). Updated both to the correct value.Added two tests in
test_decode.py: one confirming a bare decimal literal stays a float, and one confirming the exponent case still collapses to int as before.Ran the full suite locally (
python -m unittest discover -s hjson/tests), 112 tests, all passing.