Skip to content

fix: catch OverflowError in naturalsize() for very large integers - #355

Closed
koteshyelamati wants to merge 2 commits into
python-humanize:mainfrom
koteshyelamati:patch-1
Closed

koteshyelamati wants to merge 2 commits into
python-humanize:mainfrom
koteshyelamati:patch-1

Conversation

@koteshyelamati

Copy link
Copy Markdown

Problem

naturalsize() calls float(value) unconditionally, which raises OverflowError for very large integers:

>>> import humanize
>>> humanize.naturalsize(10**309)
OverflowError: int too large to convert to float

It also raises a raw ValueError from float() for non-numeric strings, with a confusing message.

Fix

Wrap float(value) in try/except (ValueError, OverflowError) and re-raise with a clear message indicating the argument type.

After fix

>>> humanize.naturalsize(10**309)
ValueError: naturalsize() argument must be a number, not 'int'
>>> humanize.naturalsize("abc")
ValueError: naturalsize() argument must be a number, not 'str'

koteshyelamati and others added 2 commits July 20, 2026 17:58
naturalsize(10**309) raises OverflowError because float(value) is called
unconditionally and int→float conversion overflows for very large integers.

Fix: wrap float(value) in try/except (ValueError, OverflowError) and re-raise
with a clear message.

Reproducer:
  >>> import humanize
  >>> humanize.naturalsize(10**309)
  OverflowError: int too large to convert to float
Comment thread src/humanize/filesize.py
Comment on lines +92 to +97
try:
bytes_ = float(value)
except (ValueError, OverflowError) as exc:
raise ValueError(
f"naturalsize() argument must be a number, not {type(value).__name__!r}"
) from exc

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.

This doesn't add support, just re-raises an OverflowError as ValueError with an incorrect error:

>>> humanize.naturalsize(10**309)
ValueError: naturalsize() argument must be a number, not 'int'

An int is a number.

>>> humanize.naturalsize("abc")
ValueError: naturalsize() argument must be a number, not 'str'

Numeric strings are documented as an acceptable input.

@hugovk hugovk closed this Sep 16, 2026
@codecov

codecov Bot commented Sep 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 50.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.35%. Comparing base (2ad3641) to head (5325357).

Files with missing lines Patch % Lines
src/humanize/filesize.py 50.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #355      +/-   ##
==========================================
- Coverage   99.56%   99.35%   -0.22%     
==========================================
  Files          12       12              
  Lines         927      930       +3     
==========================================
+ Hits          923      924       +1     
- Misses          4        6       +2     
Flag Coverage Δ
macos-latest 97.41% <50.00%> (-0.21%) ⬇️
ubuntu-latest 97.41% <50.00%> (-0.21%) ⬇️
windows-latest 95.26% <50.00%> (-0.21%) ⬇️

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.

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