diff --git a/src/humanize/filesize.py b/src/humanize/filesize.py index c495fed..3323d5e 100644 --- a/src/humanize/filesize.py +++ b/src/humanize/filesize.py @@ -2,9 +2,10 @@ from __future__ import annotations -from math import log +from math import isfinite, log from humanize.i18n import _gettext as _ +from humanize.number import _format_not_finite suffixes = { "decimal": ( @@ -88,6 +89,8 @@ def naturalsize( base = 1024 if (gnu or binary) else 1000 bytes_ = float(value) + if not isfinite(bytes_): + return _format_not_finite(bytes_) abs_bytes = abs(bytes_) if abs_bytes == 1 and not gnu: diff --git a/tests/test_filesize.py b/tests/test_filesize.py index c3e7386..0b3ecd8 100644 --- a/tests/test_filesize.py +++ b/tests/test_filesize.py @@ -93,3 +93,15 @@ def test_naturalsize(test_args: list[int] | list[int | bool], expected: str) -> test_args[0] = f"-{test_args[0]}" assert humanize.naturalsize(*test_args) == "-" + expected + + +@pytest.mark.parametrize( + "value, expected", + [ + (float("nan"), "NaN"), + (float("inf"), "+Inf"), + (float("-inf"), "-Inf"), + ], +) +def test_naturalsize_not_finite(value: float, expected: str) -> None: + assert humanize.naturalsize(value) == expected