fix(naturaldelta): catch OverflowError for float('inf')/float('-inf') (#333) - #363
fix(naturaldelta): catch OverflowError for float('inf')/float('-inf') (#333)#363Mukller wants to merge 4 commits into
Conversation
…ize#333) `int(float("inf"))` raises `OverflowError`, not `ValueError`, so the existing `except (ValueError, TypeError)` guard misses it. The function docstring states that non-finite floats are returned unchanged, but `float("inf")` and `float("-inf")` raised uncaught `OverflowError` while only `float("nan")` was silently returned. Add `OverflowError` to the except clause so that ±inf are treated the same as nan. Remove the misleading "Raises: OverflowError" note from the docstring since that exception is now caught.
Mukller
left a comment
There was a problem hiding this comment.
Code Review
Bug Verification
int(float('inf')) raises OverflowError in CPython (same on PyPy and other implementations):
>>> int(float('inf'))
OverflowError: cannot convert float infinity to integer
>>> int(float('nan'))
ValueError: cannot convert float NaN to integerThe existing except (ValueError, TypeError) handles nan and non-numeric strings but misses ±inf.
Fix Correctness
Adding OverflowError to the except clause is minimal and correct:
±infnow falls through toreturn str(value)— same behaviour asnan- Legitimate
OverflowErrorfromdt.timedelta(seconds=value)for very large finite floats is NOT affected because that line is inside thetryblock andtimedeltareceives a validfloat, notint()
Docstring
The Raises: OverflowError clause has been removed. It was already incorrect for float('inf') (it raised but should not have), and the remaining case (very large finite floats like 1e400) also now returns str(value) instead of raising, since int(1e400) raises OverflowError too.
Change Size
One exception class added to one except tuple. No logic changes.
|
This is now the 5th PR to fix this. Why is this better than #334? |
The previous fix caught all OverflowErrors, which incorrectly swallowed the documented OverflowError for truly too-large finite float values. Use math.isfinite() to distinguish inf/-inf (return unchanged) from a legitimately too-large finite value (re-raise).
…or in naturaldelta Verify inf/-inf are returned as strings, nan still works, and a truly too-large finite float still raises OverflowError as documented.
|
Thanks for the pointer to #334. I've updated this PR to use the Differences from #334:
If #334 is preferred, happy to close this one — just flag it and I'll defer. |
for more information, see https://pre-commit.ci
Problem
Fixes #333.
naturaldelta()is documented to return non-finite floats unchanged. However,float('inf')andfloat('-inf')raiseOverflowErrorinstead of returning the value as a string:Root Cause
The non-finite float guard uses
int(value)as a probe:int(value)raisesfloat('nan')ValueErrorfloat('inf')OverflowErrorfloat('-inf')OverflowError'not a float'ValueErrorFix
The misleading
Raises: OverflowErrornote is removed from the docstring since ±inf now returnsstr(value)likenandoes.After Fix