naturalsize() truncates non-integer byte counts below the base ("1 Bytes")

Open Beginner friendly
#459 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

  • #507 by @ekanshul — closed without merging

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
68/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
python
Domain
backend

Research direction

Start at the naturalsize() implementation and its tests; the issue identifies the int() conversion and singular check as the relevant behavior. Compare the formatting path for values above the base with values below it, then add tests for fractional byte counts, including gnu=True. Done means fractional inputs retain their decimal part while integer input keeps its current output.

Written by the indexing model from the issue text.

Description

On current main:

>>> import humanize
>>> humanize.naturalsize(1.5)
'1 Bytes'
>>> humanize.naturalsize(1.9)
'1 Bytes'
>>> humanize.naturalsize(1500)
'1.5 kB'

From 1 kB upwards the fraction is kept using format, but below the base the value is truncated with int(). The singular check also compares the untruncated value with 1, so 1.5 is shown as "1 Bytes".

In #239 you mentioned that "1.1 Bytes" would be better than "1 Bytes" for non-integer input, and that tests for non-integer input were missing.

Proposal:

  • Format non-integer values below the base with format, like larger units (e.g. "1.5 Bytes", and "1.5B" with gnu=True).
  • Keep integer input unchanged ("300 Bytes", "1 Byte").
  • Add tests for non-integer input.

Would you be happy with this? If so, we'd like to send a PR.

Dominant language
Python
Stars
768
Forks
243
Avg merge
4d 12h
Merged PRs (30d)
19

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python-humanize/humanize

All issues in python-humanize/humanize

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.