From 72b04d387e6c4f9209a145b124bd400f11118ed4 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Sat, 28 Sep 2024 17:24:35 -0700 Subject: [PATCH 1/5] Add passing test cases --- tests/test_filesize.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/test_filesize.py b/tests/test_filesize.py index 416cbf13..00550bd6 100644 --- a/tests/test_filesize.py +++ b/tests/test_filesize.py @@ -12,6 +12,15 @@ "test_args, expected", [ ([300], "300 Bytes"), + ([10**3], "1.0 kB"), + ([10**6], "1.0 MB"), + ([10**9], "1.0 GB"), + ([10**12], "1.0 TB"), + ([10**15], "1.0 PB"), + ([10**18], "1.0 EB"), + ([10**21], "1.0 ZB"), + ([10**27], "1.0 RB"), + ([10**30], "1.0 QB"), ([1000**1 * 31], "31.0 kB"), ([1000**2 * 32], "32.0 MB"), ([1000**3 * 33], "33.0 GB"), From f8a74b4c1342a3987d3aaa409111bcd9f3a740f7 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Sat, 28 Sep 2024 17:25:00 -0700 Subject: [PATCH 2/5] Add failing test case --- tests/test_filesize.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_filesize.py b/tests/test_filesize.py index 00550bd6..dc232d7e 100644 --- a/tests/test_filesize.py +++ b/tests/test_filesize.py @@ -19,6 +19,7 @@ ([10**15], "1.0 PB"), ([10**18], "1.0 EB"), ([10**21], "1.0 ZB"), + ([10**24], "1.0 YB"), ([10**27], "1.0 RB"), ([10**30], "1.0 QB"), ([1000**1 * 31], "31.0 kB"), From 33119c0a88a2cd1b204e660c1d2b09c5a5b8791e Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Sat, 28 Sep 2024 17:28:47 -0700 Subject: [PATCH 3/5] Fix rollover from ZB to 1.0 YB --- src/humanize/filesize.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/humanize/filesize.py b/src/humanize/filesize.py index 996506e4..9bb1a625 100644 --- a/src/humanize/filesize.py +++ b/src/humanize/filesize.py @@ -37,7 +37,7 @@ def naturalsize( gnu: bool = False, format: str = "%.1f", ) -> str: - """Format a number of bytes like a human readable filesize (e.g. 10 kB). + """Format a number of bytes like a human-readable filesize (e.g. 10 kB). By default, decimal suffixes (kB, MB) are used. @@ -83,7 +83,11 @@ def naturalsize( suffix = suffixes["decimal"] base = 1024 if (gnu or binary) else 1000 - bytes_ = float(value) + if isinstance(value, str): + bytes_ = float(value) + else: + bytes_ = value + abs_bytes = abs(bytes_) if abs_bytes == 1 and not gnu: From 32365a55041f07db830f77ed945cadcef5472977 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Sat, 28 Sep 2024 19:04:33 -0700 Subject: [PATCH 4/5] Test naturalsize with str --- tests/test_filesize.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tests/test_filesize.py b/tests/test_filesize.py index dc232d7e..e61d162d 100644 --- a/tests/test_filesize.py +++ b/tests/test_filesize.py @@ -12,6 +12,7 @@ "test_args, expected", [ ([300], "300 Bytes"), + (["1000"], "1.0 kB"), ([10**3], "1.0 kB"), ([10**6], "1.0 MB"), ([10**9], "1.0 GB"), @@ -74,9 +75,15 @@ ([10**26 * 30, True, False, "%.3f"], "2.423 RiB"), ], ) -def test_naturalsize(test_args: list[int] | list[int | bool], expected: str) -> None: +def test_naturalsize( + test_args: list[int | str] | list[int | bool], expected: str +) -> None: assert humanize.naturalsize(*test_args) == expected - args_with_negative = test_args - args_with_negative[0] *= -1 - assert humanize.naturalsize(*args_with_negative) == "-" + expected + # Retest with negative input + if isinstance(test_args[0], int): + test_args[0] *= -1 + else: + test_args[0] = f"-{test_args[0]}" + + assert humanize.naturalsize(*test_args) == "-" + expected From be8796c1d7cc53ffac3a39fc4a08e55882b05187 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Tue, 1 Oct 2024 14:20:51 +0300 Subject: [PATCH 5/5] Fix mypy --- tests/test_filesize.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/test_filesize.py b/tests/test_filesize.py index e61d162d..fbb662a9 100644 --- a/tests/test_filesize.py +++ b/tests/test_filesize.py @@ -75,9 +75,7 @@ ([10**26 * 30, True, False, "%.3f"], "2.423 RiB"), ], ) -def test_naturalsize( - test_args: list[int | str] | list[int | bool], expected: str -) -> None: +def test_naturalsize(test_args: list[int] | list[int | bool], expected: str) -> None: assert humanize.naturalsize(*test_args) == expected # Retest with negative input