Repository navigation
Preserve integer precision in fractional formatting - #510
narayana006 wants to merge 2 commits into
Conversation
|
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. |
|
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 |
fractional(2**53 + 1)returns"9007199254740992"on main, silently changing the integer.fractional(10**400)raisesOverflowErrorwhile 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 onlyfractional. The earlier combined PR #420 was closed as a duplicate of #418. This proposal covers the remainingfractionalbehavior without the overlappingapnumberchange. Integer strings and other numeric functions are outside this change.Validation on macOS / Python 3.15.2:
prek run --all-files, including Ruff and Black.git diff --checkpasses.Other Python versions and operating systems were not run locally. Hosted CI results are separate from these local checks.