Skip to content

Commit ac55bbb

Browse files
committed
test: regression for #333 non-finite float OverflowError in naturaldelta
Verify inf/-inf are returned as strings, nan still works, and a truly too-large finite float still raises OverflowError as documented.
1 parent e16584e commit ac55bbb

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

tests/test_time.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,25 @@ def test_naturaldelta(test_input: float | dt.timedelta, expected: str) -> None:
138138
assert humanize.naturaldelta(-test_input) == expected
139139

140140

141+
def test_naturaldelta_non_finite_floats() -> None:
142+
"""Regression for #333: non-finite floats must not raise OverflowError.
143+
144+
float("inf") and float("-inf") trigger int(value) -> OverflowError inside
145+
timedelta(seconds=value). They should be returned as strings, like float("nan").
146+
A legitimately too-large *finite* float must still raise OverflowError.
147+
"""
148+
import math
149+
# Non-finite values return the string repr
150+
assert humanize.naturaldelta(float("inf")) == "inf"
151+
assert humanize.naturaldelta(float("-inf")) == "-inf"
152+
# NaN already worked before; confirm it still does
153+
assert humanize.naturaldelta(float("nan")) == "nan"
154+
# A truly too-large finite float must still raise (documented behaviour)
155+
import pytest
156+
with pytest.raises(OverflowError):
157+
humanize.naturaldelta(1e308 * 10)
158+
159+
141160
@freeze_time(FROZEN_DATE)
142161
@pytest.mark.parametrize(
143162
"test_input, expected",

0 commit comments

Comments
 (0)