fix(naturaldelta): catch OverflowError for float('inf')/float('-inf') (#333) - #363
Open
Mukller wants to merge 1 commit into
Open
fix(naturaldelta): catch OverflowError for float('inf')/float('-inf') (#333)#363Mukller wants to merge 1 commit into
Mukller wants to merge 1 commit 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
commented
Jul 29, 2026
Mukller
left a comment
Author
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.
Member
|
This is now the 5th PR to fix this. Why is this better than #334? |
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
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