From c6e34447ba13fd528b42c0997242d723ca807f1a Mon Sep 17 00:00:00 2001 From: barkya28 <122352361+barkya28@users.noreply.github.com> Date: Wed, 4 Nov 2026 01:19:12 +0530 Subject: [PATCH] Clarify intcomma error for negative decimal precision Add validation, document the exception, and cover integer, float and numeric-string inputs with six regression cases. Related to python-humanize/humanize#466. Validation: 884 tests passed; Ruff, Black and mypy passed. All six regression cases fail against the original implementation. Implemented and tested with OpenAI Codex. --- src/humanize/number.py | 9 ++++++++- tests/test_number.py | 7 +++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/humanize/number.py b/src/humanize/number.py index 52a5356a..0e912346 100644 --- a/src/humanize/number.py +++ b/src/humanize/number.py @@ -169,10 +169,14 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str: Args: value (int, float, str): Integer or float to convert. - ndigits (int, None): Digits of precision for rounding after the decimal point. + ndigits (int, None): Non-negative digits of precision for rounding after the + decimal point. Returns: str: String containing commas every three digits. + + Raises: + ValueError: If ndigits is negative for a finite numeric value. """ import math @@ -195,6 +199,9 @@ def intcomma(value: NumberOrString, ndigits: int | None = None) -> str: return str(value) if ndigits is not None: + if ndigits < 0: + msg = "ndigits must be non-negative" + raise ValueError(msg) result = f"{value:,.{ndigits}f}" else: result = f"{value:,}" diff --git a/tests/test_number.py b/tests/test_number.py index 5fb12fa6..209b4a8e 100644 --- a/tests/test_number.py +++ b/tests/test_number.py @@ -83,6 +83,13 @@ def test_intcomma( assert humanize.intcomma(*test_args) == expected +@pytest.mark.parametrize("value", [12345, 12345.111, "12345.111"]) +@pytest.mark.parametrize("ndigits", [-1, -10]) +def test_intcomma_negative_precision(value: float | str, ndigits: int) -> None: + with pytest.raises(ValueError, match="^ndigits must be non-negative$"): + humanize.intcomma(value, ndigits) + + @pytest.mark.parametrize("sign", [1, -1]) def test_intcomma_large_integer(sign: int) -> None: value = sign * (10**400 + 123)