|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | import datetime as dt |
| 6 | +import math |
6 | 7 | import typing |
7 | 8 |
|
8 | 9 | import pytest |
@@ -71,6 +72,8 @@ def test_date_and_delta() -> None: |
71 | 72 | assert_equal_datetime(date, result[0]) |
72 | 73 | assert_equal_timedelta(d, result[1]) |
73 | 74 | assert time._date_and_delta("NaN") == (None, "NaN") |
| 75 | + assert time._date_and_delta(float("inf")) == (None, float("inf")) |
| 76 | + assert time._date_and_delta(float("-inf")) == (None, float("-inf")) |
74 | 77 |
|
75 | 78 |
|
76 | 79 | # Tests for the public interface of humanize.time |
@@ -128,13 +131,15 @@ def test_naturaldelta_nomonths(test_input: dt.timedelta, expected: str) -> None: |
128 | 131 | (dt.timedelta(days=365), "a year"), |
129 | 132 | (dt.timedelta(days=365 * 1_141), "1,141 years"), |
130 | 133 | ("NaN", "NaN"), # Returns non-numbers unchanged. |
| 134 | + (float("inf"), "inf"), |
| 135 | + (float("-inf"), "-inf"), |
131 | 136 | # largest possible timedelta |
132 | 137 | (dt.timedelta(days=999_999_999), "2,739,726 years"), |
133 | 138 | ], |
134 | 139 | ) |
135 | 140 | def test_naturaldelta(test_input: float | dt.timedelta, expected: str) -> None: |
136 | 141 | assert humanize.naturaldelta(test_input) == expected |
137 | | - if not isinstance(test_input, str): |
| 142 | + if not isinstance(test_input, str) and not (isinstance(test_input, float) and math.isinf(test_input)): |
138 | 143 | assert humanize.naturaldelta(-test_input) == expected |
139 | 144 |
|
140 | 145 |
|
@@ -179,6 +184,8 @@ def test_naturaldelta(test_input: float | dt.timedelta, expected: str) -> None: |
179 | 184 | (NOW - dt.timedelta(days=365 * 2 + 65), "2 years ago"), |
180 | 185 | (NOW - dt.timedelta(days=365 + 4), "1 year, 4 days ago"), |
181 | 186 | ("NaN", "NaN"), |
| 187 | + (float("inf"), "inf"), |
| 188 | + (float("-inf"), "-inf"), |
182 | 189 | ], |
183 | 190 | ) |
184 | 191 | def test_naturaltime( |
@@ -817,6 +824,8 @@ def test_precisedelta_suppress_units( |
817 | 824 |
|
818 | 825 | def test_precisedelta_bogus_call() -> None: |
819 | 826 | assert humanize.precisedelta(None) == "None" |
| 827 | + assert humanize.precisedelta(float("inf")) == "inf" |
| 828 | + assert humanize.precisedelta(float("-inf")) == "-inf" |
820 | 829 |
|
821 | 830 | with pytest.raises( |
822 | 831 | ValueError, |
|
0 commit comments