Skip to content

Handle non-finite values in naturalsize - #390

Closed
AnishPatel526 wants to merge 2 commits into
python-humanize:mainfrom
AnishPatel526:naturalsize-non-finite
Closed

AnishPatel526 wants to merge 2 commits into
python-humanize:mainfrom
AnishPatel526:naturalsize-non-finite

Conversation

@AnishPatel526

@AnishPatel526 AnishPatel526 commented Sep 5, 2026

Copy link
Copy Markdown

Changes proposed in this pull request:

  • naturalsize() now handles non-finite input consistently with the rest of the library. Every numeric formatter in humanize.number (ordinal, intcomma, intword, apnumber, fractional, scientific, clamp) already returns "NaN" / "+Inf" / "-Inf" for non-finite values via _format_not_finite. naturalsize() was the exception: naturalsize(float("nan")) raised ValueError: cannot convert float NaN to integer, and naturalsize(float("inf")) produced the bogus string "inf QB". It now returns "NaN" / "+Inf" / "-Inf".
  • Added a parametrized test_naturalsize_non_finite covering nan, inf, -inf as both floats and strings; it fails on main and passes with this change.

Before

>>> import humanize
>>> humanize.naturalsize(float("nan"))
ValueError: cannot convert float NaN to integer
>>> humanize.naturalsize(float("inf"))
'inf QB'

After

>>> humanize.naturalsize(float("nan"))
'NaN'
>>> humanize.naturalsize(float("inf"))
'+Inf'
>>> humanize.naturalsize(float("-inf"))
'-Inf'

The fix reuses the existing humanize.number._format_not_finite helper so output matches the other formatters exactly, and guards right after the float(value) conversion. Finite values are unaffected.

@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 (d205f65).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #390   +/-   ##
=======================================
  Coverage   99.56%   99.57%           
=======================================
  Files          12       12           
  Lines         927      934    +7     
=======================================
+ Hits          923      930    +7     
  Misses          4        4           
Flag Coverage Δ
macos-latest 97.64% <100.00%> (+0.01%) ⬆️
ubuntu-latest 97.64% <100.00%> (+0.01%) ⬆️
windows-latest 95.50% <100.00%> (+0.03%) ⬆️

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

Closing as duplicate of of #299.

Check for duplicates yourself next time.

@hugovk hugovk closed this Sep 16, 2026
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.

2 participants