Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/humanize/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,9 @@ def fractional(value: NumberOrString) -> str:
"""
import math

if isinstance(value, int):
return int.__repr__(value)

try:
number = float(value)
if not math.isfinite(number):
Expand Down
21 changes: 21 additions & 0 deletions tests/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -246,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",
[
Expand Down