Skip to content

Handle NaN/Inf in naturalsize#299

Open
shuofengzhang wants to merge 1 commit intopython-humanize:mainfrom
shuofengzhang:handle-non-finite-filesize
Open

Handle NaN/Inf in naturalsize#299
shuofengzhang wants to merge 1 commit intopython-humanize:mainfrom
shuofengzhang:handle-non-finite-filesize

Conversation

@shuofengzhang
Copy link

What changed

  • Return a clear string for non‑finite inputs (NaN/±Inf) in naturalsize().
  • Add tests covering NaN and ±Inf.

Why

  • naturalsize() currently computes logs on non‑finite inputs, which yields confusing output.
  • This aligns with other humanize helpers that render NaN/Inf explicitly.

Testing

  • pytest -q

@codecov
Copy link

codecov bot commented Mar 1, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.54%. Comparing base (85bb7e7) to head (c19e4f1).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #299   +/-   ##
=======================================
  Coverage   99.53%   99.54%           
=======================================
  Files          11       11           
  Lines         868      874    +6     
=======================================
+ Hits          864      870    +6     
  Misses          4        4           
Flag Coverage Δ
macos-latest 97.48% <100.00%> (+0.01%) ⬆️
ubuntu-latest 97.48% <100.00%> (+0.01%) ⬆️
windows-latest 95.88% <100.00%> (+0.02%) ⬆️

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

☔ View full report in Codecov by Sentry.
📢 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
Copy link
Member

hugovk commented Mar 1, 2026

Are there actual use cases where people have file sizes that are infinite or NaN?

@hugovk hugovk added the changelog: Added For new features label Mar 1, 2026
@shuofengzhang
Copy link
Author

shuofengzhang commented Mar 1, 2026

Are there actual use cases where people have file sizes that are infinite or NaN?

It’s mostly defensive: naturalsize() accepts floats, and callers often pass computed values (ratios, averages, deltas) from metrics/ETL pipelines. Those can become NaN/±Inf after a divide‑by‑zero or overflow, or when size is unknown in a streaming context. Returning an explicit “NaN/±Inf” is clearer than producing a weird suffix or raising. Also, humanize.number already formats NaN/Inf explicitly, so this keeps behavior consistent across helpers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog: Added For new features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants