From 0b3a52e5de0d4d278c5708b2d0244f746be3a28c Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Fri, 24 Dec 2021 22:31:31 -0800 Subject: [PATCH 1/2] TYP: localization.py --- pandas/_config/localization.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/pandas/_config/localization.py b/pandas/_config/localization.py index bc76aca93da2a..36fddef2a447a 100644 --- a/pandas/_config/localization.py +++ b/pandas/_config/localization.py @@ -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, + Generator, +) 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 +) -> Generator[str | tuple[str, str], None, None]: """ Context manager for temporarily setting a locale. @@ -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. @@ -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. @@ -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: From 788333d31206f4febba68c873fa899ed30ed94b4 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Sat, 25 Dec 2021 11:11:40 -0800 Subject: [PATCH 2/2] Add type ignore operator --- pandas/_config/localization.py | 4 ++-- pandas/tests/indexes/datetimes/test_misc.py | 3 ++- pandas/tests/scalar/timestamp/test_timestamp.py | 3 ++- pandas/tests/series/accessors/test_dt_accessor.py | 3 ++- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/pandas/_config/localization.py b/pandas/_config/localization.py index 36fddef2a447a..2a487fa4b6877 100644 --- a/pandas/_config/localization.py +++ b/pandas/_config/localization.py @@ -11,7 +11,7 @@ import subprocess from typing import ( Callable, - Generator, + Iterator, ) from pandas._config.config import options @@ -20,7 +20,7 @@ @contextmanager def set_locale( new_locale: str | tuple[str, str], lc_var: int = locale.LC_ALL -) -> Generator[str | tuple[str, str], None, None]: +) -> Iterator[str | tuple[str, str]]: """ Context manager for temporarily setting a locale. diff --git a/pandas/tests/indexes/datetimes/test_misc.py b/pandas/tests/indexes/datetimes/test_misc.py index 76b5b835754aa..d4a87e6a8b65c 100644 --- a/pandas/tests/indexes/datetimes/test_misc.py +++ b/pandas/tests/indexes/datetimes/test_misc.py @@ -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 diff --git a/pandas/tests/scalar/timestamp/test_timestamp.py b/pandas/tests/scalar/timestamp/test_timestamp.py index 03ba6b12599a6..9c9e11c6a4ba4 100644 --- a/pandas/tests/scalar/timestamp/test_timestamp.py +++ b/pandas/tests/scalar/timestamp/test_timestamp.py @@ -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 diff --git a/pandas/tests/series/accessors/test_dt_accessor.py b/pandas/tests/series/accessors/test_dt_accessor.py index 48a3ebd25c239..15e13e5567310 100644 --- a/pandas/tests/series/accessors/test_dt_accessor.py +++ b/pandas/tests/series/accessors/test_dt_accessor.py @@ -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