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

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
9 changes: 9 additions & 0 deletions src/humanize/time.py
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

__lazy_modules__ = {"humanize.i18n", "humanize.number"}

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.

from enum import Enum
from functools import total_ordering

Expand Down Expand Up @@ -153,6 +154,14 @@ def naturaldelta(
delta = dt.timedelta(seconds=value)
except (ValueError, TypeError):
return str(value)
except OverflowError:
# `int(value)` raises OverflowError for non-finite floats (inf/-inf),
# which, like NaN, are returned unchanged. A too-large *finite* value
# (whose OverflowError comes from `timedelta`) is still raised, per
# the documented `OverflowError` contract.
if not math.isfinite(value):
return str(value)
raise

use_months = months

Expand Down
13 changes: 13 additions & 0 deletions tests/test_time.py
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
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@ def test_naturaldelta(test_input: float | dt.timedelta, expected: str) -> None:
assert humanize.naturaldelta(-test_input) == expected


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"
Comment on lines +141 to +145

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



def test_naturaldelta_too_large_value_raises() -> None:
"""A too-large *finite* value still raises OverflowError (unlike inf)."""
with pytest.raises(OverflowError):
humanize.naturaldelta(1e30)


@freeze_time(FROZEN_DATE)
@pytest.mark.parametrize(
"test_input, expected",
Expand Down
Loading

Back | FazBrowse Home | New Git URL