[BUG] filesize.decimal and DownloadColumn show 1,000.0 kB instead of 1.0 MB near unit boundaries
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 82/100
Research direction
Start by reading the filesize unit-selection and formatting code, especially _to_str and pick_unit_and_suffix, then find or add tests for filesize.decimal and DownloadColumn at unit boundaries. Reproduce the reported values and check that rounding across a boundary selects the next unit while nearby values retain their current unit.
Written by the indexing model from the issue text.
Description
- I've checked docs and closed issues for possible solutions.
- I can't find my issue in the FAQ.
Describe the bug
rich.filesize.decimal picks the unit before rounding, so values just below a unit boundary get rounded up to "1,000.0" of the smaller unit instead of moving to the next unit:
from rich import filesize
print(filesize.decimal(999_499)) # 999.5 kB (fine)
print(filesize.decimal(999_950)) # 1,000.0 kB (expected 1.0 MB)
print(filesize.decimal(999_999)) # 1,000.0 kB (expected 1.0 MB)
print(filesize.decimal(999_999_999)) # 1,000.0 MB (expected 1.0 GB)
filesize.pick_unit_and_suffix has the same boundary check, so it shows up in progress bars too:
from rich.progress import DownloadColumn, Progress
progress = Progress(DownloadColumn())
progress.add_task("download", total=999_999, completed=999_999)
print(DownloadColumn().render(progress.tasks[0]).plain) # 1,000.0/1,000.0 kB
The cause is that _to_str compares the raw size against base**i but then formats size / unit with precision digits, which can round up to base. One possible fix is to move to the next unit when the rounded value would reach base, e.g. compare round(size / unit_below, precision) < base when choosing the unit. I'm happy to send a PR with tests if this approach sounds OK.
(Related, smaller: negative sizes are never scaled, since size < base is always true for them: filesize.decimal(-5_000_000) returns '-5,000,000 bytes'. I can leave that out of scope if you'd prefer.)
Platform
Click to expand
Windows 11, reproduced with rich 15.0.0 from PyPI and with current master (3a109cc). This is independent of the terminal since it's pure string formatting.
- Dominant language
- Python
- Stars
- 57.8k
- Forks
- 2.6k
- Avg merge
- 20m
- Merged PRs (30d)
- 3
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Textualize/rich
-
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Textualize/rich#4269 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Textualize/rich#4268 ·
-
Needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Textualize/rich#4267 ·
-
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
Textualize/rich#4266 ·
-
[BUG] `Text.right_crop(0)` empties the text and an amount larger than the text makes `len()` raiseOpen
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Textualize/rich#4265 ·
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
GoogleCloudPlatform/professional-services-data-validator#1899 ·
Maintainers usually reply within 4 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
mohanraj00/entelos#200 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 68/100
-
sweep
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
bketelsen/truenas-mcp#77 ·
Maintainers usually reply within 1 day
-
Console writes URL credentials to the local log, though AGENTS.md says secrets never reach diskOpen
Difficulty 1/5 Under an hour Newbie friendliness 86/100
sandbox-quantum/switch#2163 ·
Maintainers usually reply within 1 day