Skip to content

TYP: localization.py #45065

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

Merged
merged 2 commits into from
Dec 27, 2021
Merged
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
24 changes: 18 additions & 6 deletions pandas/_config/localization.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@

Name `localization` is chosen to avoid overlap with builtin `locale` module.
"""
from __future__ import annotations

from contextlib import contextmanager
import locale
import re
import subprocess
from typing import (
Callable,
Iterator,
)

from pandas._config.config import options


@contextmanager
def set_locale(new_locale, lc_var: int = locale.LC_ALL):
def set_locale(
new_locale: str | tuple[str, str], lc_var: int = locale.LC_ALL
) -> Iterator[str | tuple[str, str]]:
"""
Context manager for temporarily setting a locale.

Expand Down Expand Up @@ -71,7 +79,7 @@ def can_set_locale(lc: str, lc_var: int = locale.LC_ALL) -> bool:
return True


def _valid_locales(locales, normalize):
def _valid_locales(locales: list[str] | str, normalize: bool) -> list[str]:
"""
Return a list of normalized locales that do not throw an ``Exception``
when set.
Expand All @@ -98,11 +106,15 @@ def _valid_locales(locales, normalize):
]


def _default_locale_getter():
def _default_locale_getter() -> bytes:
return subprocess.check_output(["locale -a"], shell=True)


def get_locales(prefix=None, normalize=True, locale_getter=_default_locale_getter):
def get_locales(
prefix: str | None = None,
normalize: bool = True,
locale_getter: Callable[[], bytes] = _default_locale_getter,
) -> list[str] | None:
"""
Get all the locales that are available on the system.

Expand Down Expand Up @@ -142,9 +154,9 @@ def get_locales(prefix=None, normalize=True, locale_getter=_default_locale_gette
# raw_locales is "\n" separated list of locales
# it may contain non-decodable parts, so split
# extract what we can and then rejoin.
raw_locales = raw_locales.split(b"\n")
split_raw_locales = raw_locales.split(b"\n")
out_locales = []
for x in raw_locales:
for x in split_raw_locales:
try:
out_locales.append(str(x, encoding=options.display.encoding))
except UnicodeError:
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/indexes/datetimes/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,9 @@ def test_datetimeindex_accessors6(self):
assert [d.weekofyear for d in dates] == expected

# GH 12806
# error: Unsupported operand types for + ("List[None]" and "List[str]")
@pytest.mark.parametrize(
"time_locale", [None] if tm.get_locales() is None else [None] + tm.get_locales()
"time_locale", [None] + (tm.get_locales() or []) # type: ignore[operator]
)
def test_datetime_name_accessors(self, time_locale):
# Test Monday -> Sunday and January -> December, in that sequence
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/scalar/timestamp/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ def check(value, equal):
"data",
[Timestamp("2017-08-28 23:00:00"), Timestamp("2017-08-28 23:00:00", tz="EST")],
)
# error: Unsupported operand types for + ("List[None]" and "List[str]")
@pytest.mark.parametrize(
"time_locale", [None] if tm.get_locales() is None else [None] + tm.get_locales()
"time_locale", [None] + (tm.get_locales() or []) # type: ignore[operator]
)
def test_names(self, data, time_locale):
# GH 17354
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/series/accessors/test_dt_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,9 @@ def test_dt_accessor_no_new_attributes(self):
with pytest.raises(AttributeError, match="You cannot add any new attribute"):
ser.dt.xlabel = "a"

# error: Unsupported operand types for + ("List[None]" and "List[str]")
@pytest.mark.parametrize(
"time_locale", [None] if tm.get_locales() is None else [None] + tm.get_locales()
"time_locale", [None] + (tm.get_locales() or []) # type: ignore[operator]
)
def test_dt_accessor_datetime_name_accessors(self, time_locale):
# Test Monday -> Sunday and January -> December, in that sequence
Expand Down