FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Return non-finite floats unchanged from naturaldelta by uttam12331 · Pull Request #334 · python-humanize/humanize · GitHub

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

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:

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.py → 385 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 added the changelog: Fixed For any bug fixes label Jun 30, 2026

codecov Bot commented Jun 30, 2026
edited
Loading

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           
Flag Coverage Δ
macos-latest 97.62% <100.00%> (+0.03%) ⬆️
ubuntu-latest 97.62% <100.00%> (+0.03%) ⬆️
windows-latest 95.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 Bot commented Jun 30, 2026
edited
Loading

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 15 untouched benchmarks


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

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Thanks, just a couple of suggestions.

Comment thread tests/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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Let's parameterise:

Suggested change
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"
@pytest.mark.parametrize("value, expected", [
(float("nan"), "nan"), (float("inf"), "inf"), (float("-inf"), "-inf"),
])
def test_naturaldelta_non_finite() -> None:
"""Non-finite floats are returned unchanged instead of raising."""
assert humanize.naturaldelta(value) == expected

Comment thread src/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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Please move it inside the except OverflowError: block.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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


Back | FazBrowse Home | New Git URL