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
12 changes: 5 additions & 7 deletions src/humanize/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def _date_and_delta(
value = value if precise else round(value)
delta = dt.timedelta(seconds=value)
date = now - delta
except (ValueError, TypeError):
except (ValueError, TypeError, OverflowError):
return None, value
return date, _abs_timedelta(delta)

Expand All @@ -114,11 +114,9 @@ def naturaldelta(
Returns:
str (str or `value`): A natural representation of the amount of time
elapsed unless `value` is not datetime.timedelta or cannot be
converted to int (cannot be float due to 'inf' or 'nan').
In that case, a `value` is returned unchanged.

Raises:
OverflowError: If `value` is too large to convert to datetime.timedelta.
converted to int (cannot be float due to 'inf' or 'nan', or too
large to fit in a `datetime.timedelta`). In that case, `value` is
returned unchanged (via `str()`).
Comment on lines 115 to +119

Examples:
Compare two timestamps in a custom local timezone::
Expand Down Expand Up @@ -151,7 +149,7 @@ def naturaldelta(
int(value) # Explicitly don't support string such as "NaN" or "inf"
value = float(value)
delta = dt.timedelta(seconds=value)
except (ValueError, TypeError):
except (ValueError, TypeError, OverflowError):
return str(value)

use_months = months
Expand Down
39 changes: 39 additions & 0 deletions tests/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,45 @@ def test_time_unit() -> None:
_ = years < "foo"


@pytest.mark.parametrize(
"value, expected",
[
(float("inf"), "inf"),
(float("-inf"), "-inf"),
(float("nan"), "nan"),
],
)
def test_naturaldelta_non_finite(value: float, expected: str) -> None:
# Regression test for #333: non-finite floats used to raise an uncaught
# OverflowError (or, for nan, were only handled when passed as a string)
# instead of being returned unchanged like other non-numeric input.
Comment on lines +849 to +851
assert humanize.naturaldelta(value) == expected


@pytest.mark.parametrize(
"value, expected",
[
(float("inf"), "inf"),
(float("-inf"), "-inf"),
(float("nan"), "nan"),
],
)
def test_naturaltime_non_finite(value: float, expected: str) -> None:
assert humanize.naturaltime(value) == expected


@pytest.mark.parametrize(
"value, expected",
[
(float("inf"), "inf"),
(float("-inf"), "-inf"),
(float("nan"), "nan"),
],
)
def test_precisedelta_non_finite(value: float, expected: str) -> None:
assert humanize.precisedelta(value) == expected


@pytest.mark.parametrize(
"fmt, value, expected",
[
Expand Down