{{ message }}
fix: avoid ValueError/OverflowError on NaN/Inf with significant_digits=0#604
Open
gaoflow wants to merge 3 commits into
Open
fix: avoid ValueError/OverflowError on NaN/Inf with significant_digits=0#604gaoflow wants to merge 3 commits into
gaoflow wants to merge 3 commits into
Conversation
number_to_string called int() on non-finite float values (NaN, Inf,
-Inf) when significant_digits=0, raising ValueError or OverflowError.
Guard the int() conversion with math.isfinite() so that NaN and Inf
pass through as-is (their string representations compare correctly).
Fixes the crash:
>>> DeepDiff(float('nan'), float('nan'), significant_digits=0)
ValueError: cannot convert float NaN to integer
>>> DeepDiff(float('inf'), float('inf'), significant_digits=0)
OverflowError: cannot convert float infinity to integer
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.

Problem
DeepDiffcrashes withValueErrororOverflowErrorwhen comparing NaN or Inf float values withsignificant_digits=0:The crash also occurs when NaN/Inf values are nested inside dicts, lists, or other structures.
Root Cause
In
number_to_string, whensignificant_digits=0, the code callsint(number)on the rounded value. For NaN and Inf,round()returns NaN/Inf unchanged, andint()raises because these values cannot be converted to integers.Fix
Guard the
int()conversion withmath.isfinite()so that NaN and Inf values pass through without attempting integer conversion. Their string representations ("nan", "inf", "-inf") will compare correctly in the existing string-comparison path.The fix is a one-line change:
if significant_digits == 0becomesif significant_digits == 0 and math.isfinite(number).Additional Note
When
significant_digits > 0, NaN and Inf do not crash becauseround(nan, n)returns NaN and the string formatting produces "nan" which compares equal to another "nan". This behavior is preserved.