From 94684b94fce7cd7a5ba3dfcb53648ad0e90a7acc Mon Sep 17 00:00:00 2001 From: narayana006 <69020110+narayana006@users.noreply.github.com> Date: Tue, 10 Nov 2026 23:00:14 -0500 Subject: [PATCH 1/2] Preserve integer precision in fractional formatting --- src/humanize/number.py | 3 +++ tests/test_number.py | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/src/humanize/number.py b/src/humanize/number.py index 52a5356a..1dfabdad 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -380,6 +380,9 @@ def fractional(value: NumberOrString) -> str: """ import math + if isinstance(value, int): + return str(int(value)) + try: number = float(value) if not math.isfinite(number): diff --git a/tests/test_number.py b/tests/test_number.py index 5fb12fa6..f18eee9d 100644 --- a/tests/test_number.py +++ b/tests/test_number.py @@ -216,6 +216,12 @@ def test_apnumber(test_input: int | str, expected: str) -> None: "test_input, expected", [ (1, "1"), + (True, "1"), + (False, "0"), + (2**53 + 1, "9007199254740993"), + (-(2**53 + 1), "-9007199254740993"), + (10**400, "1" + "0" * 400), + (-(10**400), "-1" + "0" * 400), (2.0, "2"), (4.0 / 3.0, "1 1/3"), (5.0 / 6.0, "5/6"), From a50914c6a56168f51a0c4e6fb32308ff4c107110 Mon Sep 17 00:00:00 2001 From: narayana006 <69020110+narayana006@users.noreply.github.com> Date: Thu, 19 Nov 2026 09:28:45 -0500 Subject: [PATCH 2/2] Preserve stored values of int subclasses in fractional --- src/humanize/number.py | 2 +- tests/test_number.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/humanize/number.py b/src/humanize/number.py index 1dfabdad..8f2e0f0f 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -381,7 +381,7 @@ def fractional(value: NumberOrString) -> str: import math if isinstance(value, int): - return str(int(value)) + return int.__repr__(value) try: number = float(value) diff --git a/tests/test_number.py b/tests/test_number.py index f18eee9d..178628a3 100644 --- a/tests/test_number.py +++ b/tests/test_number.py @@ -252,6 +252,21 @@ def test_fractional(test_input: float | str, expected: str) -> None: assert humanize.fractional(test_input) == expected +@pytest.mark.parametrize("value", [7, -7, 2**53 + 1, -(2**53 + 1), 10**400]) +def test_fractional_int_subclass_preserves_stored_value(value: int) -> None: + class TaggedInt(int): + def __int__(self) -> int: + return 42 + + def __str__(self) -> str: + return "custom string" + + def __repr__(self) -> str: + return "custom representation" + + assert humanize.fractional(TaggedInt(value)) == str(value) + + @pytest.mark.parametrize( "test_args, expected", [