Skip to content

Commit 12cfabb

Browse files
authored
TYP: localization.py (#45065)
1 parent 81cdbb4 commit 12cfabb

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

pandas/_config/localization.py

+18-6
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,24 @@
33
44
Name `localization` is chosen to avoid overlap with builtin `locale` module.
55
"""
6+
from __future__ import annotations
7+
68
from contextlib import contextmanager
79
import locale
810
import re
911
import subprocess
12+
from typing import (
13+
Callable,
14+
Iterator,
15+
)
1016

1117
from pandas._config.config import options
1218

1319

1420
@contextmanager
15-
def set_locale(new_locale, lc_var: int = locale.LC_ALL):
21+
def set_locale(
22+
new_locale: str | tuple[str, str], lc_var: int = locale.LC_ALL
23+
) -> Iterator[str | tuple[str, str]]:
1624
"""
1725
Context manager for temporarily setting a locale.
1826
@@ -71,7 +79,7 @@ def can_set_locale(lc: str, lc_var: int = locale.LC_ALL) -> bool:
7179
return True
7280

7381

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

100108

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

104112

105-
def get_locales(prefix=None, normalize=True, locale_getter=_default_locale_getter):
113+
def get_locales(
114+
prefix: str | None = None,
115+
normalize: bool = True,
116+
locale_getter: Callable[[], bytes] = _default_locale_getter,
117+
) -> list[str] | None:
106118
"""
107119
Get all the locales that are available on the system.
108120
@@ -142,9 +154,9 @@ def get_locales(prefix=None, normalize=True, locale_getter=_default_locale_gette
142154
# raw_locales is "\n" separated list of locales
143155
# it may contain non-decodable parts, so split
144156
# extract what we can and then rejoin.
145-
raw_locales = raw_locales.split(b"\n")
157+
split_raw_locales = raw_locales.split(b"\n")
146158
out_locales = []
147-
for x in raw_locales:
159+
for x in split_raw_locales:
148160
try:
149161
out_locales.append(str(x, encoding=options.display.encoding))
150162
except UnicodeError:

pandas/tests/indexes/datetimes/test_misc.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,9 @@ def test_datetimeindex_accessors6(self):
191191
assert [d.weekofyear for d in dates] == expected
192192

193193
# GH 12806
194+
# error: Unsupported operand types for + ("List[None]" and "List[str]")
194195
@pytest.mark.parametrize(
195-
"time_locale", [None] if tm.get_locales() is None else [None] + tm.get_locales()
196+
"time_locale", [None] + (tm.get_locales() or []) # type: ignore[operator]
196197
)
197198
def test_datetime_name_accessors(self, time_locale):
198199
# Test Monday -> Sunday and January -> December, in that sequence

pandas/tests/scalar/timestamp/test_timestamp.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,9 @@ def check(value, equal):
154154
"data",
155155
[Timestamp("2017-08-28 23:00:00"), Timestamp("2017-08-28 23:00:00", tz="EST")],
156156
)
157+
# error: Unsupported operand types for + ("List[None]" and "List[str]")
157158
@pytest.mark.parametrize(
158-
"time_locale", [None] if tm.get_locales() is None else [None] + tm.get_locales()
159+
"time_locale", [None] + (tm.get_locales() or []) # type: ignore[operator]
159160
)
160161
def test_names(self, data, time_locale):
161162
# GH 17354

pandas/tests/series/accessors/test_dt_accessor.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,9 @@ def test_dt_accessor_no_new_attributes(self):
426426
with pytest.raises(AttributeError, match="You cannot add any new attribute"):
427427
ser.dt.xlabel = "a"
428428

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

0 commit comments

Comments
 (0)