Skip to content

Return non-finite floats unchanged from naturaldelta - #334

Merged
hugovk merged 7 commits into
python-humanize:mainfrom
uttam12331:fix/333-naturaldelta-inf
Aug 29, 2026
Merged

Return non-finite floats unchanged from naturaldelta#334
hugovk merged 7 commits into
python-humanize:mainfrom
uttam12331:fix/333-naturaldelta-inf

Conversation

@uttam12331

Copy link
Copy Markdown
Contributor

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:

except OverflowError:
    if not math.isfinite(value):
        return str(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.

Closes python-humanize#333
@hugovk hugovk added the changelog: Fixed For any bug fixes label Jun 30, 2026
@codecov

codecov Bot 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 (08cf2c3) to head (18415fa).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #334   +/-   ##
=======================================
  Coverage   99.56%   99.56%           
=======================================
  Files          12       12           
  Lines         916      927   +11     
=======================================
+ Hits          912      923   +11     
  Misses          4        4           
Flag Coverage Δ
macos-latest 97.62% <100.00%> (+0.02%) ⬆️
ubuntu-latest 97.62% <100.00%> (+0.02%) ⬆️
windows-latest 95.46% <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-hq Bot commented Jun 30, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 15 untouched benchmarks


Comparing uttam12331:fix/333-naturaldelta-inf (18415fa) with main (08cf2c3)

Open in CodSpeed

@hugovk hugovk 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 thread tests/test_time.py Outdated
Comment thread src/humanize/time.py Outdated
Comment thread src/humanize/time.py
hugovk and others added 3 commits August 29, 2026 20:19
@hugovk hugovk closed this Aug 29, 2026
@hugovk hugovk reopened this Aug 29, 2026
@hugovk
hugovk enabled auto-merge (squash) August 29, 2026 17:24
@hugovk
hugovk merged commit b48b37b into python-humanize:main Aug 29, 2026
43 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog: Fixed For 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