Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix i18n _get_default_locale_path #204

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
22 changes: 13 additions & 9 deletions src/humanize/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from __future__ import annotations

import gettext as gettext_module
import os.path
import importlib.resources
import os
import pathlib
from threading import local
Comment on lines +6 to 9
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import importlib.resources
import os
import pathlib
from threading import local
from threading import local
from typing import TYPE_CHECKING
if TYPE_CHECKING:
import os
import pathlib


__all__ = ["activate", "deactivate", "decimal_separator", "thousands_separator"]
Expand Down Expand Up @@ -32,14 +34,14 @@
}


def _get_default_locale_path() -> str | None:
try:
if __file__ is None:
return None
return os.path.join(os.path.dirname(__file__), "locale")
except NameError:
def _get_default_locale_path() -> pathlib.Path | None:
package = __spec__ and __spec__.parent
if not package:
return None

with importlib.resources.as_file(importlib.resources.files(package)) as pkg:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
with importlib.resources.as_file(importlib.resources.files(package)) as pkg:
import importlib.resources
with importlib.resources.as_file(importlib.resources.files(package)) as pkg:

return pkg / "locale"


def get_translation() -> gettext_module.NullTranslations:
try:
Expand All @@ -48,14 +50,16 @@ def get_translation() -> gettext_module.NullTranslations:
return _TRANSLATIONS[None]


def activate(locale: str, path: str | None = None) -> gettext_module.NullTranslations:
def activate(
locale: str, path: str | os.PathLike[str] | None = None
) -> gettext_module.NullTranslations:
"""Activate internationalisation.
Set `locale` as current locale. Search for locale in directory `path`.
Args:
locale (str): Language name, e.g. `en_GB`.
path (str): Path to search for locales.
path (str | pathlib.Path): Path to search for locales.
Returns:
dict: Translations.
Expand Down
22 changes: 13 additions & 9 deletions tests/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,20 @@ def test_ordinal_genders(
humanize.i18n.deactivate()


def test_default_locale_path_defined__file__() -> None:
def test_default_locale_path_defined__spec__() -> None:
i18n = importlib.import_module("humanize.i18n")
assert i18n._get_default_locale_path() is not None


def test_default_locale_path_null__file__() -> None:
def test_default_locale_path_none__spec__(monkeypatch: pytest.MonkeyPatch) -> None:
i18n = importlib.import_module("humanize.i18n")
i18n.__file__ = None
monkeypatch.setattr(i18n, "__spec__", None)
assert i18n._get_default_locale_path() is None


def test_default_locale_path_undefined__file__() -> None:
def test_default_locale_path_undefined__file__(monkeypatch: pytest.MonkeyPatch) -> None:
i18n = importlib.import_module("humanize.i18n")
del i18n.__file__
monkeypatch.delattr(i18n, "__spec__")
assert i18n._get_default_locale_path() is None


Expand All @@ -179,17 +179,21 @@ class TestActivate:
" 'locale' folder. You need to pass the path explicitly."
)

def test_default_locale_path_null__file__(self) -> None:
def test_default_locale_path_null__spec__(
self, monkeypatch: pytest.MonkeyPatch
) -> None:
i18n = importlib.import_module("humanize.i18n")
i18n.__file__ = None
monkeypatch.setattr(i18n, "__spec__", None)

with pytest.raises(Exception) as excinfo:
i18n.activate("ru_RU")
assert str(excinfo.value) == self.expected_msg

def test_default_locale_path_undefined__file__(self) -> None:
def test_default_locale_path_undefined__spec__(
self, monkeypatch: pytest.MonkeyPatch
) -> None:
i18n = importlib.import_module("humanize.i18n")
del i18n.__file__
monkeypatch.delattr(i18n, "__spec__")

with pytest.raises(Exception) as excinfo:
i18n.activate("ru_RU")
Expand Down
Loading