Skip to content

Return NaN/Inf unchanged from naturalsize() - #389

Closed
AbhiramMandala wants to merge 5 commits into
python-humanize:mainfrom
AbhiramMandala:fix-naturalsize-nan-inf
Closed

AbhiramMandala wants to merge 5 commits into
python-humanize:mainfrom
AbhiramMandala:fix-naturalsize-nan-inf

Conversation

@AbhiramMandala

Copy link
Copy Markdown

naturalsize() raised ValueError for math.nan and produced malformed output like 'inf QB' for math.inf, unlike other numeric humanizers that consistently return 'NaN', '+Inf', or '-Inf' for non-finite input via number._format_not_finite().

Add the same isfinite() short-circuit to naturalsize(), reusing number._format_not_finite() for consistent formatting, plus docstring examples and a dedicated parametrized test.

Changes proposed in this pull request:

  • Short-circuit naturalsize() with isfinite(bytes_) immediately after bytes_ = float(value), before any exponent/log math runs on the value.
  • Reuse number._format_not_finite() for the non-finite return value instead of duplicating nan/inf string formatting, keeping naturalsize() consistent with intcomma, intword, apnumber, fractional, scientific, clamp, metric, and naturaldelta.
  • Add three pycon doctest examples (naturalsize(float("nan")), naturalsize(float("inf")), naturalsize(float("-inf"))) to the function's docstring.
  • Add a dedicated test_naturalsize_not_finite parametrized test covering math.nan, math.inf, and -math.inf across the default, binary=True, and gnu=True modes.

Abhiram Mandala and others added 2 commits September 3, 2026 17:29
naturalsize() raised ValueError for math.nan and produced malformed output like 'inf QB' for math.inf, unlike other numeric humanizers that consistently return 'NaN', '+Inf', or '-Inf' for non-finite input via number._format_not_finite().

Add the same isfinite() short-circuit to naturalsize(), reusing number._format_not_finite() for consistent formatting, plus docstring examples and a dedicated parametrized test.

@AbhiramMandala AbhiramMandala left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @hugovk, when you get a chance, could you please review this PR? I’d appreciate any feedback. Thanks!

@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
@AbhiramMandala
AbhiramMandala deleted the fix-naturalsize-nan-inf branch September 16, 2026 16:07
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