Repository: humanize (difficulty: medium)

Issue:
naturalsize() mishandles values that equal exactly one unit boundary. naturalsize(1000) returns '1000 Bytes' instead of '1.0 kB', and in binary mode naturalsize(1024, binary=True) returns '1024 Bytes' instead of '1.0 KiB'. Values just below the boundary (e.g. 999, 1023) are correctly shown in bytes, and values just above (1001) are correctly scaled -- only the exact boundary value is wrong. It should switch to the scaled unit as soon as the value reaches the base, not only once it exceeds it.

Source files:
filesize.py: """Bits and bytes related humanization.

Vendored verbatim from humanize 4.15.0 (filesize.naturalsize), MIT licensed.
"""
from __future__ import annotations

from math import log

suffixes = {
    "decimal": (
        " kB", " MB", " GB", " TB", " PB",
        " EB", " ZB", " YB", " RB", " QB",
    ),
    "binary": (
        " KiB", " MiB", " GiB", " TiB", " PiB",
        " EiB", " ZiB", " YiB", " RiB", " QiB",
    ),
    "gnu": "KMGTPEZYRQ",
}


def naturalsize(
    value: float | str,
    binary: bool = False,
    gnu: bool = False,
    format: str = "%.1f",
) -> str:
    """Format a number of bytes like a human-readable filesize (e.g. 10 kB).

    By default, decimal suffixes (kB, MB) are used.

    Non-GNU modes are compatible with jinja2's `filesizeformat` filter.

    Examples:
        ```pycon
        >>> naturalsize(3000000)
        '3.0 MB'
        >>> naturalsize(300, False, True)
        '300B'
        >>> naturalsize(3000, False, True)
        '2.9K'
        >>> naturalsize(3000, False, True, "%.3f")
        '2.930K'
        >>> naturalsize(3000, True)
        '2.9 KiB'
        >>> naturalsize(10**28)
        '10.0 RB'
        >>> naturalsize(10**34 * 3)
        '30000.0 QB'
        >>> naturalsize(-4096, True)
        '-4.0 KiB'

        ```

    Args:
        value (int, float, str): Integer to convert.
        binary (bool): If `True`, uses binary suffixes (KiB, MiB) with base
            2<sup>10</sup> instead of 10<sup>3</sup>.
        gnu (bool): If `True`, the binary argument is ignored and GNU-style
            (`ls -sh` style) prefixes are used (K, M) with the 2**10 definition.
        format (str): Custom formatter.

    Returns:
        str: Human readable representation of a filesize.
    """
    if gnu:
        suffix = suffixes["gnu"]
    elif binary:
        suffix = suffixes["binary"]
    else:
        suffix = suffixes["decimal"]

    base = 1024 if (gnu or binary) else 1000
    bytes_ = float(value)
    abs_bytes = abs(bytes_)

    if abs_bytes == 1 and not gnu:
        return f"{int(bytes_)} Byte"

    if abs_bytes <= base:
        return f"{int(bytes_)}B" if gnu else f"{int(bytes_)} Bytes"

    exp = int(min(log(abs_bytes, base), len(suffix)))
    ret: str = format % (bytes_ / (base**exp)) + suffix[exp - 1]
    return ret


Test files:
test_filesize.py: """Tests for filesize humanizing. Slice of humanize 4.15.0 test_filesize.py."""
from __future__ import annotations

import pytest

from filesize import naturalsize


@pytest.mark.parametrize(
    "test_args, expected",
    [
        ([300], "300 Bytes"),
        ([999], "999 Bytes"),
        (["1000"], "1.0 kB"),
        ([10**3], "1.0 kB"),
        ([1001], "1.0 kB"),
        ([10**6], "1.0 MB"),
        ([1024, True], "1.0 KiB"),
        ([1023, True], "1023 Bytes"),
        ([3000, False, True], "2.9K"),
        ([1024, False, True], "1.0K"),
        ([1023, False, True], "1023B"),
    ],
)
def test_naturalsize(test_args, expected):
    assert naturalsize(*test_args) == expected


Patch:
--- a/filesize.py
+++ b/filesize.py
@@ -77,7 +77,7 @@
     if abs_bytes == 1 and not gnu:
         return f"{int(bytes_)} Byte"
 
-    if abs_bytes <= base:
+    if abs_bytes < base:
         return f"{int(bytes_)}B" if gnu else f"{int(bytes_)} Bytes"
 
     exp = int(min(log(abs_bytes, base), len(suffix)))