Skip to content

Fix naturalsize() ValueError for custom formats containing text - #367

Open
Sanjays2402 wants to merge 1 commit into
python-humanize:mainfrom
Sanjays2402:fix/naturalsize-custom-format
Open

Fix naturalsize() ValueError for custom formats containing text#367
Sanjays2402 wants to merge 1 commit into
python-humanize:mainfrom
Sanjays2402:fix/naturalsize-custom-format

Conversation

@Sanjays2402

Copy link
Copy Markdown

Fixes #366

Changes proposed in this pull request:

  • naturalsize()'s unit-rollover check ran the user-supplied format through float(), so any format with text around the conversion (e.g. "Size: %.1f") raised ValueError — a regression from 4.15.0.
  • Compare the rendered mantissa against the rendered base instead, keeping the 999999 -> 1.0 MB carry-over while accepting arbitrary formats again.
  • Added two parametrized cases to tests/test_filesize.py; they fail with ValueError without the fix and pass with it.

This change was prepared with AI assistance; the regression test was run locally and fails without the fix.

naturalsize()'s unit rollover check ran the user-supplied format string
through float(), which raises ValueError whenever that format contains
text around the numeric conversion (e.g. "Size: %.1f"), a regression
from 4.15.0.

Compare the rendered mantissa against the rendered base instead, so the
carry-over still works and arbitrary formats are accepted again.

Closes python-humanize#366
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

1 participant