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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/humanize/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ def _date_and_delta(
date = now - delta
except (ValueError, TypeError):
return None, value
except OverflowError:
# `round(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 (mirrors naturaldelta, #334).
import math

if not math.isfinite(value):
return None, value
raise
return date, _abs_timedelta(delta)


Expand Down Expand Up @@ -284,6 +295,8 @@ def naturaltime(

Returns:
str: A natural representation of the input in a resolution that makes sense.
Non-finite floats ('inf', '-inf', 'nan') are returned unchanged
as their string, like in `naturaldelta`.
"""
import datetime as dt

Expand Down Expand Up @@ -544,6 +557,9 @@ def precisedelta(
'0 minutes'

```

Non-finite floats ('inf', '-inf', 'nan') are returned unchanged as their
string, like in `naturaldelta`.
"""
date, delta = _date_and_delta(value, precise=True)
if date is None:
Expand Down
40 changes: 40 additions & 0 deletions tests/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def test_date_and_delta() -> None:
assert_equal_datetime(date, result[0])
assert_equal_timedelta(d, result[1])
assert time._date_and_delta("NaN") == (None, "NaN")
assert time._date_and_delta(float("inf")) == (None, float("inf"))
assert time._date_and_delta(float("inf"), precise=True) == (None, float("inf"))


# Tests for the public interface of humanize.time
Expand Down Expand Up @@ -162,6 +164,25 @@ def test_naturaldelta_too_large_value_raises() -> None:
humanize.naturaldelta(1e30)


@pytest.mark.parametrize(
"value, expected",
[
(float("nan"), "nan"),
(float("inf"), "inf"),
(float("-inf"), "-inf"),
],
)
def test_naturaltime_non_finite(value: float, expected: str) -> None:
"""Non-finite floats are returned unchanged instead of raising."""
assert humanize.naturaltime(value) == expected


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


@freeze_time(FROZEN_DATE)
@pytest.mark.parametrize(
"test_input, expected",
Expand Down Expand Up @@ -568,6 +589,25 @@ def test_naturaltime_timezone_when(test_input: dt.datetime, expected: str) -> No
assert humanize.naturaltime(test_input, when=NOW_UTC) == expected


@pytest.mark.parametrize(
"value, expected",
[
(float("nan"), "nan"),
(float("inf"), "inf"),
(float("-inf"), "-inf"),
],
)
def test_precisedelta_non_finite(value: float, expected: str) -> None:
"""Non-finite floats are returned unchanged instead of raising."""
assert humanize.precisedelta(value) == expected


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


@pytest.mark.parametrize(
"val, min_unit, expected",
[
Expand Down