Skip to content

fix naturalsize ValueError for custom format strings with text (fixes #366) - #396

Closed
aDragon0707 wants to merge 2 commits into
python-humanize:mainfrom
aDragon0707:fix-366
Closed

aDragon0707 wants to merge 2 commits into
python-humanize:mainfrom
aDragon0707:fix-366

Conversation

@aDragon0707

Copy link
Copy Markdown

Fixes #366.

naturalsize() raised ValueError for custom format strings with surrounding text (e.g. format="Size: %.1f"), because the rollover check passed the full formatted result to float().

  • The rollover step now tries to parse the formatted mantissa as a float and skips the rollover when the formatted result is not a plain number, preserving the pre-4.16 behavior for text formats.
  • Plain numeric formats keep the rollover behavior (999_999 -> "1.0 MB" still works).

Regression test added (test_naturalsize_custom_format_with_text): prefix and suffix cases.

tests/test_filesize.py → 77 passed, 0 failed.

@codecov

codecov Bot commented Sep 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.57%. Comparing base (3201e70) to head (93e7b85).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #396   +/-   ##
=======================================
  Coverage   99.56%   99.57%           
=======================================
  Files          12       12           
  Lines         927      936    +9     
=======================================
+ Hits          923      932    +9     
  Misses          4        4           
Flag Coverage Δ
macos-latest 97.64% <100.00%> (+0.02%) ⬆️
ubuntu-latest 97.64% <100.00%> (+0.02%) ⬆️
windows-latest 95.51% <100.00%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@hugovk

hugovk commented Sep 16, 2026

Copy link
Copy Markdown
Member

Fixes #366.

If you open this issue, you'll see there's already an open PR for this: #367.

Check next time, I'm getting so many duplicates that it wastes review time that can be spent elsewhere.

@hugovk hugovk closed this Sep 16, 2026
Comment thread tests/test_filesize.py
humanize.naturalsize(999_999, gnu=True, format="Size: %.1f") == "Size: 976.6K"
)
assert (
humanize.naturalsize(999_999, gnu=True, format="%.1f bytes") == "976.6 bytesK"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

"976.6 bytesK" is nonsense! 🙃

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.

naturalsize() raises ValueError for custom format strings with text

2 participants