| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
…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.
There was a problem hiding this comment.
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.
Adding OverflowError to the except clause is minimal and correct:
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.
One exception class added to one except tuple. No logic changes.
Sorry, something went wrong.
|
This is now the 5th PR to fix this. Why is this better than #334? |
Sorry, something went wrong.
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 math.isfinite() guard from that approach instead of the blanket except OverflowError — the original version incorrectly swallowed OverflowError for legitimately too-large finite floats, which violates the documented contract. Differences from #334:
If #334 is preferred, happy to close this one — just flag it and I'll defer. |
Sorry, something went wrong.
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
|
Good point, thank you for flagging that. Both PRs fix the same issue (#333) with a similar approach — catching OverflowError and using math.isinf to distinguish non-finite floats from legitimately too-large finite values. Looking at #334: it's been open since June 2026 and appears complete and correct. If you prefer that one, I'm happy to close this PR. If #334 is stale or you'd prefer to take this one instead, I can incorporate any additional feedback you have. |
Sorry, something went wrong.
|
One data point for the triage: #334 has had no commits or author activity since June 30 (~8 weeks), while this PR was rebased recently and passes CI. Whichever you prefer to keep — happy to defer to #334 as offered above, or incorporate anything from it here. |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Problem
Fixes #333.
naturaldelta() is documented to return non-finite floats unchanged. However, float('inf') and float('-inf') raise OverflowError instead of returning the value as a string:
Root Cause
The non-finite float guard uses int(value) as a probe:
Fix
The misleading Raises: OverflowError note is removed from the docstring since ±inf now returns str(value) like nan does.
After Fix