From 872d7331fe3e307d3cc682d422f51f2de13c0fa7 Mon Sep 17 00:00:00 2001 From: Willy Polychenko Date: Fri, 2 Feb 2024 19:33:25 +0200 Subject: [PATCH 1/5] Use float to support sub-second precision --- src/humanize/time.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/humanize/time.py b/src/humanize/time.py index 52828c1..e8cb410 100644 --- a/src/humanize/time.py +++ b/src/humanize/time.py @@ -104,7 +104,7 @@ 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. In that case, a `value` is returned unchanged. + converted to float. In that case, a `value` is returned unchanged. Raises: OverflowError: If `value` is too large to convert to datetime.timedelta. @@ -131,7 +131,7 @@ def naturaldelta( delta = value else: try: - value = int(value) + value = float(value) delta = dt.timedelta(seconds=value) except (ValueError, TypeError): return str(value) From b60e5558da85cd324021f4fed98872145a9c76e5 Mon Sep 17 00:00:00 2001 From: Willy Polychenko Date: Fri, 2 Feb 2024 20:10:56 +0200 Subject: [PATCH 2/5] Verify timedelta & float inputs are correct --- tests/test_time.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_time.py b/tests/test_time.py index 0396be2..18cc241 100644 --- a/tests/test_time.py +++ b/tests/test_time.py @@ -126,10 +126,9 @@ def test_naturaldelta_nomonths(test_input: dt.timedelta, expected: str) -> None: (dt.timedelta(days=999_999_999), "2,739,726 years"), ], ) -def test_naturaldelta(test_input: int | dt.timedelta, expected: str) -> None: +def test_naturaldelta(test_input: float | dt.timedelta, expected: str) -> None: assert humanize.naturaldelta(test_input) == expected - @freeze_time("2020-02-02") @pytest.mark.parametrize( "test_input, expected", @@ -339,6 +338,7 @@ def test_naturaldelta_minimum_unit_explicit( # Act / Assert assert humanize.naturaldelta(delta, minimum_unit=minimum_unit) == expected + assert humanize.naturaldelta(seconds, minimum_unit=minimum_unit) == expected @freeze_time("2020-02-02") From 475c5eaf5d0147c72283d2c72f808d770bbd58c4 Mon Sep 17 00:00:00 2001 From: Willy Date: Fri, 2 Feb 2024 20:36:06 +0200 Subject: [PATCH 3/5] Explicitly don't support special floats --- src/humanize/time.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/humanize/time.py b/src/humanize/time.py index e8cb410..c5700e9 100644 --- a/src/humanize/time.py +++ b/src/humanize/time.py @@ -104,7 +104,8 @@ 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 float. In that case, a `value` is returned unchanged. + 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. @@ -131,6 +132,7 @@ def naturaldelta( delta = value else: try: + int(value) # Explicitly don't support string such as "NaN" or "inf" value = float(value) delta = dt.timedelta(seconds=value) except (ValueError, TypeError): From 3fbb88c702e01464eb67c7ff3690839f66bcee6e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 2 Feb 2024 18:42:07 +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 | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_time.py b/tests/test_time.py index 18cc241..edb3498 100644 --- a/tests/test_time.py +++ b/tests/test_time.py @@ -129,6 +129,7 @@ def test_naturaldelta_nomonths(test_input: dt.timedelta, expected: str) -> None: def test_naturaldelta(test_input: float | dt.timedelta, expected: str) -> None: assert humanize.naturaldelta(test_input) == expected + @freeze_time("2020-02-02") @pytest.mark.parametrize( "test_input, expected", From c94f9512c86c095080366cf1dabb576e40c94ad7 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Sat, 24 Feb 2024 04:12:20 -0700 Subject: [PATCH 5/5] Fix typo --- src/humanize/time.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/humanize/time.py b/src/humanize/time.py index c5700e9..9a60e01 100644 --- a/src/humanize/time.py +++ b/src/humanize/time.py @@ -104,7 +104,7 @@ 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). + converted to int (cannot be float due to 'inf' or 'nan'). In that case, a `value` is returned unchanged. Raises: