Skip to content

fix: catch OverflowError for non-finite floats in time functions - #375

Open
Manushprajwal7 wants to merge 1 commit into
python-humanize:mainfrom
Manushprajwal7:fix/naturaldelta-overflow-non-finite
Open

fix: catch OverflowError for non-finite floats in time functions#375
Manushprajwal7 wants to merge 1 commit into
python-humanize:mainfrom
Manushprajwal7:fix/naturaldelta-overflow-non-finite

Conversation

@Manushprajwal7

Copy link
Copy Markdown

Summary

Fixes #333.

naturaldelta(), naturaltime(), and precisedelta() all raise an uncaught OverflowError for float('inf') / float('-inf') instead of returning the value unchanged — inconsistent with every other numeric humanize function (ordinal, intcomma, intword, etc.), which already treat non-finite input this way, and inconsistent with how these same three functions already handle float('nan').

>>> import humanize
>>> humanize.naturaldelta(float("inf"))
Traceback (most recent call last):
    ...
OverflowError: cannot convert float infinity to integer

Root cause: int() / round() raise OverflowError (not ValueError or TypeError) for infinite floats. That exception wasn't included in the except clauses guarding the timedelta conversion in naturaldelta(), nor in the shared _date_and_delta() helper used by naturaltime() and precisedelta(). Since both call sites share the same root cause, this fixes all three functions rather than just the one named in the issue.

Changes

  • src/humanize/time.py: add OverflowError to the two relevant except clauses (in naturaldelta() and _date_and_delta()).
  • Updated naturaldelta()'s docstring, which documented the crash as a Raises: OverflowError — that was describing the bug, not an intentional design.
  • Added regression tests for inf/-inf/nan across naturaldelta, naturaltime, and precisedelta.

Test plan

  • pytest tests/test_time.py -q — 392 passed
  • Full suite: pytest -q — 724 passed, 74 skipped, 0 failed
  • ruff check on changed files — clean
  • Manually verified naturaldelta/naturaltime/precisedelta no longer raise for inf/-inf, and behavior for genuinely-too-large-but-finite values (e.g. 1e300) is now consistent (returned unchanged rather than raising)

naturaldelta(), naturaltime(), and precisedelta() all raised an uncaught
OverflowError for float('inf') / float('-inf') instead of returning the
value unchanged, unlike every other numeric humanize function (which
already treat non-finite input this way) and unlike how these same
functions already handle float('nan').

The root cause is that int()/round() raise OverflowError (not ValueError
or TypeError) for infinite floats, and that exception wasn't in the
except clauses guarding the timedelta conversion in naturaldelta() and
the shared _date_and_delta() helper used by naturaltime() and
precisedelta().

Fixes python-humanize#333.
Copilot AI lite review requested due to automatic review settings August 9, 2026 06:38

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an inconsistency in the time humanizing APIs where non-finite floats (inf/-inf) could raise an uncaught OverflowError. The change aligns naturaldelta(), naturaltime(), and precisedelta() with other humanize numeric functions by returning a string representation for non-finite/invalid inputs rather than raising.

Changes:

  • Catch OverflowError in naturaldelta() and the shared _date_and_delta() helper.
  • Update naturaldelta() docstring to describe the intended behavior (no longer documenting the crash as an expected exception).
  • Add regression tests covering inf/-inf/nan for naturaldelta, naturaltime, and precisedelta.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/humanize/time.py Extends conversion guards to include OverflowError and adjusts documentation around non-finite/too-large values.
tests/test_time.py Adds regression coverage for non-finite float inputs across the affected public time functions.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/humanize/time.py
Comment on lines 115 to +119
str (str or `value`): A natural representation of the amount of time
elapsed unless `value` is not datetime.timedelta or cannot be
converted to int (cannot be float due to 'inf' or 'nan').
In that case, a `value` is returned unchanged.

Raises:
OverflowError: If `value` is too large to convert to datetime.timedelta.
converted to int (cannot be float due to 'inf' or 'nan', or too
large to fit in a `datetime.timedelta`). In that case, `value` is
returned unchanged (via `str()`).
Comment thread src/humanize/time.py
@@ -151,7 +149,7 @@ def naturaldelta(
int(value) # Explicitly don't support string such as "NaN" or "inf"
Comment thread tests/test_time.py
Comment on lines +849 to +851
# Regression test for #333: non-finite floats used to raise an uncaught
# OverflowError (or, for nan, were only handled when passed as a string)
# instead of being returned unchanged like other non-numeric input.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

naturaldelta() raises OverflowError on float('inf') instead of returning it unchanged

2 participants