Repository navigation
Conversation
intcomma() and intword() already swap "." for decimal_separator(). naturalsize() left the mantissa dotted, so fr_FR / de_DE / it_IT showed "1.5 Mo" instead of "1,5 Mo". Rewrite only when the locale separator is not ".", so the default English path stays a single format + concat. Fixes python-humanize#460
Muguett-DBY
left a comment
There was a problem hiding this comment.
I reproduced a cache leak in the new regression and suggested a small cleanup below.
Checked exact head b0f07f0 on Windows / Python 3.12.14 with selected test bodies and a small test catalog. Codex-assisted; I didn't run pytest/tox or compile .mo files.
| def test_naturalsize_uses_locale_decimal_separator() -> None: | ||
| """Locales with a comma radix should not keep a dotted mantissa. | ||
|
|
||
| Uses a NullTranslations catalog so this does not depend on compiled .mo files. | ||
| """ | ||
| humanize.i18n._TRANSLATIONS["fr_FR"] = gettext.NullTranslations() | ||
| humanize.i18n._CURRENT.locale = "fr_FR" | ||
| try: | ||
| assert humanize.naturalsize(1_500_000) == "1,5 MB" | ||
| assert humanize.naturalsize(3000, gnu=True) == "2,9K" | ||
| assert humanize.naturalsize(3000, binary=True) == "2,9 KiB" | ||
| finally: | ||
| humanize.i18n.deactivate() | ||
| assert humanize.naturalsize(1_500_000) == "1.5 MB" |
There was a problem hiding this comment.
Please scope this cache entry with the monkeypatch fixture. deactivate() only resets the current locale; it leaves the NullTranslations object in _TRANSLATIONS["fr_FR"]. A later activate("fr_FR") then reuses the dummy catalog instead of loading or retaining the French translations.
Running this test body before the existing French naturalsize(42) case gives 42 Bytes instead of 42 octets. I reproduced this with the French cache entry absent and with a previously cached French catalog. monkeypatch.setitem restores the previous entry, or removes the inserted key, at teardown; test_update_translations already uses that pattern.
| def test_naturalsize_uses_locale_decimal_separator() -> None: | |
| """Locales with a comma radix should not keep a dotted mantissa. | |
| Uses a NullTranslations catalog so this does not depend on compiled .mo files. | |
| """ | |
| humanize.i18n._TRANSLATIONS["fr_FR"] = gettext.NullTranslations() | |
| humanize.i18n._CURRENT.locale = "fr_FR" | |
| try: | |
| assert humanize.naturalsize(1_500_000) == "1,5 MB" | |
| assert humanize.naturalsize(3000, gnu=True) == "2,9K" | |
| assert humanize.naturalsize(3000, binary=True) == "2,9 KiB" | |
| finally: | |
| humanize.i18n.deactivate() | |
| assert humanize.naturalsize(1_500_000) == "1.5 MB" | |
| def test_naturalsize_uses_locale_decimal_separator( | |
| monkeypatch: pytest.MonkeyPatch, | |
| ) -> None: | |
| """Locales with a comma radix should not keep a dotted mantissa. | |
| Uses a NullTranslations catalog so this does not depend on compiled .mo files. | |
| """ | |
| monkeypatch.setitem( | |
| humanize.i18n._TRANSLATIONS, "fr_FR", gettext.NullTranslations() | |
| ) | |
| humanize.i18n._CURRENT.locale = "fr_FR" | |
| try: | |
| assert humanize.naturalsize(1_500_000) == "1,5 MB" | |
| assert humanize.naturalsize(3000, gnu=True) == "2,9K" | |
| assert humanize.naturalsize(3000, binary=True) == "2,9 KiB" | |
| finally: | |
| humanize.i18n.deactivate() | |
| assert humanize.naturalsize(1_500_000) == "1.5 MB" |
Fixes #460
Changes proposed in this pull request:
decimal_separator()innaturalsize()so locales with a comma radix (fr_FR, de_DE, it_IT, …) render1,5 Moinstead of1.5 Mo"."so the default English path stays a single format + concatintcomma()andintword()already honordecimal_separator(). #378 leftnaturalsizeas the remaining loose end.