Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/humanize/filesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ def naturalsize(
return _("%d Byte") % int(bytes_)

if abs_bytes < base:
return f"{int(bytes_)}B" if gnu else _("%d Bytes") % int(bytes_)
if bytes_.is_integer():
return f"{int(bytes_)}B" if gnu else _("%d Bytes") % int(bytes_)
formatted_bytes = format % bytes_
return f"{formatted_bytes}B" if gnu else f"{formatted_bytes} Bytes"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-GNU fractional values bypass _() here and always receive the English label Bytes. With the real fr_FR catalog active:

from humanize.filesize import naturalsize
from humanize.i18n import activate

activate("fr_FR")
naturalsize(2)    # '2 octets'
naturalsize(1.5)  # '1.5 Bytes'

The base returned '1 octets' for the fractional call, so preserving the fraction now drops its existing unit translation. Negative fractions and binary=True take the same path. Please retain a translatable message for this branch and add a compiled-catalog regression.


from math import log

Expand Down
6 changes: 4 additions & 2 deletions tests/test_filesize.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@
([3000, False, True, "%.3f"], "2.930K"),
([3000000000, False, True, "%.0f"], "3G"),
([10**26 * 30, True, False, "%.3f"], "2.423 RiB"),
([1.123456789], "1 Bytes"),
([1.5], "1.5 Bytes"),
([1.9], "1.9 Bytes"),
([1.123456789 * 10**3], "1.1 kB"),
([1.123456789 * 10**6], "1.1 MB"),
([1.123456789, False, True], "1B"),
([1.5, False, True], "1.5B"),
([1.9, False, True], "1.9B"),
([1.123456789 * 10**3, False, True], "1.1K"),
([1.123456789 * 10**6, False, True], "1.1M"),
# Rounding must not leave the mantissa at the base while a larger suffix
Expand Down