`naturalsize()` raises `ValueError` for custom format strings with text
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 75/100
Research direction
Start at the naturalsize() entry point and inspect the unit-rollover handling introduced by PR #329, especially the custom-format path described in the report. Add regression coverage for format strings with both prefixes and suffixes, then run the relevant test suite; done means these formats preserve the pre-4.16 behavior while rollover still works for plain numeric formats.
Written by the indexing model from the issue text.
Description
Environment
- humanize 4.16.0 and current
mainat42b4a1db - Python 3.11.15
Reproduction
import humanize
print(humanize.naturalsize(999_999, gnu=True, format="Size: %.1f"))
humanize 4.15.0 returns:
Size: 976.6K
humanize 4.16.0 and current main raise:
ValueError: could not convert string to float: 'Size: 976.6'
Expected behavior
The documented custom formatter should continue to accept text around the numeric conversion.
Since this behavior worked in 4.15.0, preserving it would avoid a backward compatibility regression.
Cause
PR #329 added unit rollover handling. The new condition formats the mantissa with the
user-provided format and passes the entire result to float():
float(format % (abs_bytes / (base**exp)))
This works for a plain numeric format such as "%.1f", but not for a general formatter
such as "Size: %.1f".
Suggested fix
It might be preferable not to require the complete user-formatted result to be parseable as a float. One
backward-compatible option is to apply rollover only when the formatted result is a
plain numeric string, and otherwise preserve the pre-4.16 behavior. A more complete
solution could derive the rounded numeric mantissa independently from surrounding text.
It might also be worth adding a regression test covering both a prefix and a suffix.
How this was found
This was found by change-directed differential testing: reviewing the recent rollover change, mapping it to the public naturalsize() API, and constructing a documented custom formatter that reaches the newly added float(format % value) expression.
Related: #329, which fixes rollover but does not discuss custom format strings.
Thanks for taking the time to review this report! I appreciate all the work that goes into maintaining this project.
- Dominant language
- Python
- Stars
- 757
- Forks
- 148
- Avg merge
- 6d 1h
- Merged PRs (30d)
- 12
Contributor 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 python-humanize/humanize
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
python-humanize/humanize#379 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 52/100
python-humanize/humanize#356 · 2 comments ·
-
Difficulty 3/5 1-2 days Newbie friendliness 48/100
python-humanize/humanize#277 · 1 comment ·
-
bug
Difficulty 4/5 3-5 days Newbie friendliness 45/100
python-humanize/humanize#270 · 6 comments ·
-
help wanted
Difficulty 4/5 3-5 days Newbie friendliness 35/100
python-humanize/humanize#263 · 3 reactions ·
All issues in python-humanize/humanize
Similar issues
-
documentation help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
simonw/sqlite-utils#872 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100