From eb090eb58d806b4cc512f6fb985f2a1ccc227d1f Mon Sep 17 00:00:00 2001 From: Denis Botanin Date: Sat, 31 Oct 2026 10:04:54 +0000 Subject: [PATCH 1/2] test: cover remaining uncovered branches in i18n and number --- tests/test_i18n.py | 22 ++++++++++++++++++++++ tests/test_number.py | 22 ++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/tests/test_i18n.py b/tests/test_i18n.py index 0d3ced84..207fffc7 100644 --- a/tests/test_i18n.py +++ b/tests/test_i18n.py @@ -470,3 +470,25 @@ def test_intword_unit_has_no_format_placeholder(locale: str) -> None: pytest.skip("Generate .mo with scripts/generate-translation-binaries.sh") finally: humanize.i18n.deactivate() + + + +def test_activate_populates_translation_cache(monkeypatch: pytest.MonkeyPatch) -> None: + """activate() caches translation on first call (cache-miss branch).""" + import gettext + + fake = gettext.NullTranslations() + monkeypatch.setattr( + humanize.i18n.gettext_module, + "translation", + lambda *args, **kwargs: fake, + ) + humanize.i18n._TRANSLATIONS.pop("de_DE", None) + try: + result = humanize.i18n.activate("de_DE") + assert result is fake + assert humanize.i18n._TRANSLATIONS["de_DE"] is fake + assert humanize.i18n._CURRENT.locale == "de_DE" + finally: + humanize.i18n._TRANSLATIONS.pop("de_DE", None) + humanize.i18n.deactivate() diff --git a/tests/test_number.py b/tests/test_number.py index 5fb12fa6..a52cc671 100644 --- a/tests/test_number.py +++ b/tests/test_number.py @@ -377,3 +377,25 @@ def test_clamp(test_args: list[typing.Any], expected: str) -> None: ) def test_metric(test_args: list[typing.Any], expected: str) -> None: assert humanize.metric(*test_args) == expected + + +def test_format_not_finite_returns_empty_for_finite() -> None: + """_format_not_finite falls back to empty string for finite values.""" + assert number._format_not_finite(42.0) == "" + assert number._format_not_finite(0) == "" + assert number._format_not_finite(-1.5) == "" + + + +def test_intcomma_with_custom_separators(monkeypatch: pytest.MonkeyPatch) -> None: + """intcomma uses thousands_separator/decimal_separator helpers.""" + monkeypatch.setattr(number, "thousands_separator", lambda: ".") + monkeypatch.setattr(number, "decimal_separator", lambda: ",") + assert humanize.intcomma(1234) == "1.234" + assert humanize.intcomma(1234.5, ndigits=2) == "1.234,50" + assert humanize.intcomma("14308,40", ndigits=1) == "14.308,4" +def test_clamp_invalid_format_raises() -> None: + """clamp raises ValueError for non-string, non-callable format.""" + with pytest.raises(ValueError, match="Invalid format"): + humanize.clamp(42, format=123) # type: ignore[arg-type] + From 7b10427bfa630d747094584a3ce0051332edc875 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 31 Oct 2026 10:09:55 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_i18n.py | 1 - tests/test_number.py | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/test_i18n.py b/tests/test_i18n.py index 207fffc7..37c88c51 100644 --- a/tests/test_i18n.py +++ b/tests/test_i18n.py @@ -472,7 +472,6 @@ def test_intword_unit_has_no_format_placeholder(locale: str) -> None: humanize.i18n.deactivate() - def test_activate_populates_translation_cache(monkeypatch: pytest.MonkeyPatch) -> None: """activate() caches translation on first call (cache-miss branch).""" import gettext diff --git a/tests/test_number.py b/tests/test_number.py index a52cc671..96c7e64c 100644 --- a/tests/test_number.py +++ b/tests/test_number.py @@ -386,7 +386,6 @@ def test_format_not_finite_returns_empty_for_finite() -> None: assert number._format_not_finite(-1.5) == "" - def test_intcomma_with_custom_separators(monkeypatch: pytest.MonkeyPatch) -> None: """intcomma uses thousands_separator/decimal_separator helpers.""" monkeypatch.setattr(number, "thousands_separator", lambda: ".") @@ -394,8 +393,9 @@ def test_intcomma_with_custom_separators(monkeypatch: pytest.MonkeyPatch) -> Non assert humanize.intcomma(1234) == "1.234" assert humanize.intcomma(1234.5, ndigits=2) == "1.234,50" assert humanize.intcomma("14308,40", ndigits=1) == "14.308,4" + + def test_clamp_invalid_format_raises() -> None: """clamp raises ValueError for non-string, non-callable format.""" with pytest.raises(ValueError, match="Invalid format"): humanize.clamp(42, format=123) # type: ignore[arg-type] -