Skip to content

Return non-finite floats unchanged from naturaldelta - #334

Open
uttam12331 wants to merge 3 commits into
python-humanize:mainfrom
uttam12331:fix/333-naturaldelta-inf
Open

Return non-finite floats unchanged from naturaldelta#334
uttam12331 wants to merge 3 commits into
python-humanize:mainfrom
uttam12331:fix/333-naturaldelta-inf

Conversation

@uttam12331

Copy link
Copy Markdown

Summary

Closes#333.

naturaldelta()'s docstring says values it cannot convert to a timedelta are returned unchanged, and float("nan") already is. But float("inf") / float("-inf") raised an uncaught OverflowError (from int(value)) instead of being returned unchanged like nan:

>>> humanize.naturaldelta(float("nan"))
'nan'
>>> humanize.naturaldelta(float("inf"))
OverflowError: cannot convert float infinity to integer # before
'inf' # after

Fix

Catch OverflowError and return the value unchanged only when it is non-finite. A too-large finite value — whose OverflowError comes from timedelta(seconds=...) — is still re-raised, preserving the documented Raises: OverflowError contract:

exceptOverflowError:
ifnotmath.isfinite(value):
returnstr(value)
raise

Tests

  • test_naturaldelta_non_finite — asserts nan/inf/-inf return "nan"/"inf"/"-inf".
  • test_naturaldelta_too_large_value_raises — locks in that a too-large finite value (1e30) still raises OverflowError.

Verified locally: pytest tests/test_time.py385 passed; ruff check clean.

naturaldelta() documents that values it cannot convert are returned
unchanged, and float('nan') already is. But float('inf')/float('-inf')
raised an uncaught OverflowError from int(value), instead of being returned
unchanged like nan.
Catch OverflowError and return non-finite floats unchanged. A too-large
*finite* value, whose OverflowError comes from timedelta(), is still raised,
preserving the documented OverflowError contract.
Closespython-humanize#333
@hugovkhugovk added the changelog: Fixed For any bug fixes label Jun 30, 2026
@codecov

codecovBot commented Jun 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.56%. Comparing base (3c577d7) to head (99e251f).

Additional details and impacted files
@@ Coverage Diff @@## main #334 +/- ##
=======================================
Coverage 99.56% 99.56% =======================================
Files 12 12 Lines 913 925 +12 =======================================
+ Hits 909 921 +12 
Misses 4 4 
FlagCoverage Δ
macos-latest97.62% <100.00%> (+0.03%)⬆️
ubuntu-latest97.62% <100.00%> (+0.03%)⬆️
windows-latest95.78% <100.00%> (+0.05%)⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed-hq

codspeed-hqBot commented Jun 30, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 15 untouched benchmarks


Comparing uttam12331:fix/333-naturaldelta-inf (99e251f) with main (3c577d7)

Open in CodSpeed

@hugovkhugovk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, just a couple of suggestions.

Comment threadtests/test_time.py
Comment on lines +141 to +145
def test_naturaldelta_non_finite() -> None:
"""Non-finite floats are returned unchanged instead of raising (#333)."""
assert humanize.naturaldelta(float("nan")) == "nan"
assert humanize.naturaldelta(float("inf")) == "inf"
assert humanize.naturaldelta(float("-inf")) == "-inf"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's parameterise:

Suggested change
deftest_naturaldelta_non_finite() ->None:
"""Non-finite floats are returned unchanged instead of raising (#333)."""
asserthumanize.naturaldelta(float("nan")) =="nan"
asserthumanize.naturaldelta(float("inf")) =="inf"
asserthumanize.naturaldelta(float("-inf")) =="-inf"
@pytest.mark.parametrize("value, expected", [
(float("nan"), "nan"), (float("inf"), "inf"), (float("-inf"), "-inf"),
])
deftest_naturaldelta_non_finite() ->None:
"""Non-finite floats are returned unchanged instead of raising."""
asserthumanize.naturaldelta(value) ==expected

Comment threadsrc/humanize/time.py

from __future__ import annotations

import math

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's defer this so it's not imported if not needed.

Please move it inside the except OverflowError: block.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog: FixedFor any bug fixes

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

@uttam12331@hugovk