| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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.
There was a problem hiding this comment.
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:
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.
Sorry, something went wrong.
| 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()`). |
| # 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. |
|
Heads-up for maintainers: this PR and #375 fix the same OverflowError-on-non-finite issue (#333) but with opposite semantics for finite too-large values:
These will behave differently for e.g. naturaldelta(1e20) (finite, exceeds timedelta range): this PR raises, #375 returns "1e+20". Whichever semantics the maintainer prefers, this PR and #373 diverge as described above, so one should likely be closed or reworked once semantics are chosen — flagging early since neither references the other. (Also noting for completeness that I reviewed #374 separately; unrelated.) |
Sorry, something went wrong.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
for more information, see https://pre-commit.ci
| Back | FazBrowse Home | New Git URL |
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').
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
Test plan