Skip to content

fix: preserve fractional bytes in naturalsize - #462

Open
manshi-n wants to merge 1 commit into
python-humanize:mainfrom
manshi-n:fix/naturalsize-fractional-bytes
Open

manshi-n wants to merge 1 commit into
python-humanize:mainfrom
manshi-n:fix/naturalsize-fractional-bytes

Conversation

@manshi-n

Copy link
Copy Markdown

Summary

  • Preserve fractional byte counts below the base instead of truncating them to integers.
  • Keep existing output for integer byte counts.
  • Add regression tests for fractional values in standard and GNU formats.

Testing

  • py -3.13 -m pytest -q — 746 passed, 112 skipped.
  • git diff --check — passed.

@Ekko82 Ekko82 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The new fractional-byte branch drops the translated unit label.

Reproduced on Windows with Python 3.13.9 at c18458aaa04cfac2d434b42f51573b4428909f0a, using the actual filesize and i18n modules and the checked-in French catalog compiled with Babel. The English/GNU fractional cases preserve their fractions, and integer French outputs remain unchanged. The full suite and package builds were not run.

Comment thread src/humanize/filesize.py
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants