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

Utilize __init_subclass__ instead of inspect on Locale mapping to improve type checking #920

Merged
merged 3 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 11 additions & 17 deletions arrow/locales.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import inspect
import sys
from math import trunc
from typing import (
Expand Down Expand Up @@ -48,6 +47,9 @@
]


_locale_map: Dict[str, Type["Locale"]] = dict()


def get_locale(name: str) -> "Locale":
"""Returns an appropriate :class:`Locale <arrow.locales.Locale>`
corresponding to an input locale name.
Expand All @@ -56,7 +58,7 @@ def get_locale(name: str) -> "Locale":

"""

locale_cls = _locales.get(name.lower())
locale_cls = _locale_map.get(name.lower())

if locale_cls is None:
raise ValueError(f"Unsupported locale {name!r}.")
Expand Down Expand Up @@ -121,6 +123,13 @@ class Locale:

_month_name_to_ordinal: Optional[Dict[str, int]]

def __init_subclass__(cls, **kwargs: Any) -> None:
for locale_name in cls.names:
if locale_name in _locale_map:
Copy link
Member

Choose a reason for hiding this comment

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

Should we maintain the locale_name.lower() like in the old code?

raise LookupError(f"Duplicated locale name: {locale_name}")
jadchaar marked this conversation as resolved.
Show resolved Hide resolved

_locale_map[locale_name] = cls

def __init__(self) -> None:

self._month_name_to_ordinal = None
Expand Down Expand Up @@ -3405,18 +3414,6 @@ class MarathiLocale(Locale):
day_abbreviations = ["", "सोम", "मंगळ", "बुध", "गुरु", "शुक्र", "शनि", "रवि"]


def _map_locales() -> Dict[str, Type[Locale]]:

locales: Dict[str, Type[Locale]] = {}

for _, cls in inspect.getmembers(sys.modules[__name__], inspect.isclass):
if issubclass(cls, Locale): # pragma: no branch
for name in cls.names:
locales[name.lower()] = cls

return locales


class CatalanLocale(Locale):
names = ["ca", "ca_es", "ca_ad", "ca_fr", "ca_it"]
past = "Fa {0}"
Expand Down Expand Up @@ -4384,6 +4381,3 @@ class SwahiliLocale(Locale):
"Jumamosi",
"Jumapili",
]


_locales: Dict[str, Type[Locale]] = _map_locales()
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def arrow_factory(request):

@pytest.fixture(scope="class")
def lang_locales(request):
request.cls.locales = locales._locales
request.cls.locales = locales._locale_map


@pytest.fixture(scope="class")
Expand Down
4 changes: 2 additions & 2 deletions tests/test_locales.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_get_locale(self, mocker):
with pytest.raises(ValueError):
arrow.locales.get_locale("locale_name")

cls_dict = arrow.locales._locales
cls_dict = arrow.locales._locale_map
mocker.patch.dict(cls_dict, {"locale_name": mock_locale_cls})

result = arrow.locales.get_locale("locale_name")
Expand All @@ -68,7 +68,7 @@ def test_get_locale_by_class_name(self, mocker):

def test_locales(self):

assert len(locales._locales) > 0
assert len(locales._locale_map) > 0


@pytest.mark.usefixtures("lang_locale")
Expand Down