Skip to content

Preserve integer precision in fractional formatting - #510

Open
narayana006 wants to merge 2 commits into
python-humanize:mainfrom
narayana006:fix/fractional-integer-precision
Open

narayana006 wants to merge 2 commits into
python-humanize:mainfrom
narayana006:fix/fractional-integer-precision

Conversation

@narayana006

@narayana006 narayana006 commented Nov 11, 2026 •

Copy link
Copy Markdown

fractional(2**53 + 1) returns "9007199254740992" on main, silently changing the integer. fractional(10**400) raises OverflowError while converting an already integral input to a float.

Format stored integer values directly with int.__repr__ before float conversion. This preserves exact large integers and the existing "1" / "0" output for booleans, while bypassing overridden integer conversion and string/repr hooks on subclasses. Includes positive and negative large-integer regressions, boolean controls, and five subclass regression cases.

Related to #419. The current diff of #418 changes only apnumber; this PR changes only fractional. The earlier combined PR #420 was closed as a duplicate of #418. This proposal covers the remaining fractional behavior without the overlapping apnumber change. Integer strings and other numeric functions are outside this change.

Validation on macOS / Python 3.15.2:

  • The four original large-integer regressions fail on unchanged main; five new subclass regressions fail on the previous PR head.
  • After compiling translation catalogs, the complete suite passes: 889 passed, including benchmark functions exercised with timing disabled and localization tests.
  • Strict mypy passes for all 14 files.
  • All applicable pinned hooks pass with prek run --all-files, including Ruff and Black.
  • git diff --check passes.

Other Python versions and operating systems were not run locally. Hosted CI results are separate from these local checks.

@robertwmccarty

Copy link
Copy Markdown

I checked 330 signed integer inputs around powers of two. Base d16e635 changed the displayed integer on 218. Head 94684b9 matches str(n) on all 330.

One narrow compatibility decision: isinstance(value, int) also selects subclasses that override int. An object with underlying integer value 7 used to display 7 and now displays 42 in this constructed example. If the aim is to display the stored integer value, int.repr(value) bypasses the conversion hook and still gives 1/0 for booleans.

from humanize import fractional
values = [s*(2**p+d) for p in range(50,65) for d in range(-5,6) for s in (-1,1)]
print(sum(fractional(n) != str(n) for n in values), len(values))

class Tagged(int):
    def __int__(self):
        return 42

print(fractional(Tagged(7)), int.__repr__(Tagged(7)))

Base prints 218 330 and 7 7. Head prints 0 330 and 42 7. AI-assisted review, run locally on CPython 3.12.14.

@robertwmccarty

Copy link
Copy Markdown

I reran the review cases against a wheel built from a50914c on CPython 3.12.14. All 330 signed integers around powers of two displayed exactly. All five subclass values displayed their stored integers with int, str and repr overridden, including the original 7 case. True and False still displayed as 1 and 0. This resolves my review concern. Thanks for adding the regression cases.

Buddy from buddylists.dev · AI-assisted verification

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