From 99ac0591e28aca6eeb1cbeba096ead3bc0c5150b Mon Sep 17 00:00:00 2001 From: uttam12331 Date: Mon, 29 Jun 2026 20:47:10 +0530 Subject: [PATCH 1/5] Return non-finite floats unchanged from naturaldelta naturaldelta() documents that values it cannot convert are returned unchanged, and float('nan') already is. But float('inf')/float('-inf') raised an uncaught OverflowError from int(value), instead of being returned unchanged like nan. Catch OverflowError and return non-finite floats unchanged. A too-large *finite* value, whose OverflowError comes from timedelta(), is still raised, preserving the documented OverflowError contract. Closes #333 --- src/humanize/time.py | 9 +++++++++ tests/test_time.py | 13 +++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/humanize/time.py b/src/humanize/time.py index 8651a981..be73bd76 100644 --- a/src/humanize/time.py +++ b/src/humanize/time.py @@ -5,6 +5,7 @@ from __future__ import annotations +import math from enum import Enum from functools import total_ordering @@ -151,6 +152,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 diff --git a/tests/test_time.py b/tests/test_time.py index 76997704..b3f448c2 100644 --- a/tests/test_time.py +++ b/tests/test_time.py @@ -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" + + +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", From 99e251f0434f7eca41a2faa8aed811b0b473d744 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 30 Jun 2026 16:14:11 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/humanize/time.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/humanize/time.py b/src/humanize/time.py index ad519fc6..5cec1770 100644 --- a/src/humanize/time.py +++ b/src/humanize/time.py @@ -8,7 +8,6 @@ __lazy_modules__ = {"humanize.i18n", "humanize.number"} import math - from enum import Enum from functools import total_ordering From f684e1d58ed8bd2b20b12bdc0460e8b4682b744d Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Sat, 29 Aug 2026 20:19:30 +0300 Subject: [PATCH 3/5] Apply batched suggestions from code review Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- src/humanize/time.py | 3 ++- tests/test_time.py | 9 +++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/humanize/time.py b/src/humanize/time.py index 5cec1770..975cc03c 100644 --- a/src/humanize/time.py +++ b/src/humanize/time.py @@ -7,7 +7,6 @@ __lazy_modules__ = {"humanize.i18n", "humanize.number"} -import math from enum import Enum from functools import total_ordering @@ -159,6 +158,8 @@ def naturaldelta( # which, like NaN, are returned unchanged. A too-large *finite* value # (whose OverflowError comes from `timedelta`) is still raised, per # the documented `OverflowError` contract. + import math + if not math.isfinite(value): return str(value) raise diff --git a/tests/test_time.py b/tests/test_time.py index b3f448c2..7184cc9f 100644 --- a/tests/test_time.py +++ b/tests/test_time.py @@ -138,11 +138,12 @@ def test_naturaldelta(test_input: float | dt.timedelta, expected: str) -> None: assert humanize.naturaldelta(-test_input) == expected +@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 (#333).""" - assert humanize.naturaldelta(float("nan")) == "nan" - assert humanize.naturaldelta(float("inf")) == "inf" - assert humanize.naturaldelta(float("-inf")) == "-inf" + """Non-finite floats are returned unchanged instead of raising.""" + assert humanize.naturaldelta(value) == expected def test_naturaldelta_too_large_value_raises() -> None: From 8c5d6a5326f88cba73a8fde4d1199597af8c6b19 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 29 Aug 2026 17:19:42 +0000 Subject: [PATCH 4/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_time.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/test_time.py b/tests/test_time.py index 7184cc9f..47679409 100644 --- a/tests/test_time.py +++ b/tests/test_time.py @@ -138,9 +138,14 @@ def test_naturaldelta(test_input: float | dt.timedelta, expected: str) -> None: assert humanize.naturaldelta(-test_input) == expected -@pytest.mark.parametrize("value, expected", [ - (float("nan"), "nan"), (float("inf"), "inf"), (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 From 18415fa01e477bd8d42c15d65745af38dca5afb2 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Sat, 29 Aug 2026 20:23:12 +0300 Subject: [PATCH 5/5] Fix test params --- tests/test_time.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_time.py b/tests/test_time.py index 47679409..c3743bbc 100644 --- a/tests/test_time.py +++ b/tests/test_time.py @@ -146,7 +146,7 @@ def test_naturaldelta(test_input: float | dt.timedelta, expected: str) -> None: (float("-inf"), "-inf"), ], ) -def test_naturaldelta_non_finite() -> None: +def test_naturaldelta_non_finite(value: float, expected: str) -> None: """Non-finite floats are returned unchanged instead of raising.""" assert humanize.naturaldelta(value) == expected