filesize: raise ValueError for non-finite bytes in naturalsize#349
Closed
HrachShah wants to merge 3 commits into
Closed
filesize: raise ValueError for non-finite bytes in naturalsize#349HrachShah wants to merge 3 commits into
HrachShah wants to merge 3 commits into
Conversation
naturalsize(float('nan')) fell through to int(log(abs(nan), base)) and
raised a confusing 'cannot convert float NaN to integer' from int() on
the suffix exponent, far from the user's call site. naturalsize(+/-inf)
silently rendered as 'inf QB' / '-inf QB', a stringified float pasted in
front of a binary-scaled suffix.
Reject both up front with math.isfinite(bytes_) and a clear
ValueError naming the offending value. Strings like 'inf' / 'NaN' /
'-inf' go through float() first and trip the same check.
Adds 8 new test cases (3 floats + 5 strings) and 2 doctest examples.
for more information, see https://pre-commit.ci
Member
|
Blocking and reporting @HrachShah for deleting CI here and python-attrs/attrs#1585 and opening duplicate PRs (#342) and updating that PR while my question. |
This was referenced Jul 9, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
naturalsize(float('nan'))raisedValueError: cannot convert float NaN to integerfrom inside theint(min(log(...)))exponent calculation, which is several levels removed from the call site.naturalsize(float('inf'))andnaturalsize(float('-inf'))silently rendered as'inf QB'/'-inf QB', which is not a meaningful byte count for any real caller.Fix
Reject NaN, +inf, and -inf up front with a single
math.isfinite(bytes_)check that names the rejected value in the error message. The same check covers string inputs thatfloat()accepts but that don't represent a meaningful byte count ('inf','INF','NaN', etc.); non-numeric strings still reach the existingfloat()call and raise ValueError as before.Tests
Eight new cases in
tests/test_filesize.py:test_naturalsize_rejects_non_finite: parametrized overfloat('nan'),float('inf'),float('-inf').test_naturalsize_rejects_non_finite_strings: parametrized over'nan','inf','-inf','NaN','INF'.Both assert that
pytest.raises(ValueError, match='finite number of bytes')fires, so the fix is checked end-to-end on every relevant shape of non-finite input.Three new doctest cases in
naturalsize:naturalsize(0)(theabs < basepath was previously untested), and explicitTraceback/ValueErrorcases fornaturalsize(float('nan'))andnaturalsize(float('inf')).Full suite: 723 passed, 69 skipped (no regressions).