3
3
4
4
Name `localization` is chosen to avoid overlap with builtin `locale` module.
5
5
"""
6
+ from __future__ import annotations
7
+
6
8
from contextlib import contextmanager
7
9
import locale
8
10
import re
9
11
import subprocess
12
+ from typing import (
13
+ Callable ,
14
+ Iterator ,
15
+ )
10
16
11
17
from pandas ._config .config import options
12
18
13
19
14
20
@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 ]]:
16
24
"""
17
25
Context manager for temporarily setting a locale.
18
26
@@ -71,7 +79,7 @@ def can_set_locale(lc: str, lc_var: int = locale.LC_ALL) -> bool:
71
79
return True
72
80
73
81
74
- def _valid_locales (locales , normalize ) :
82
+ def _valid_locales (locales : list [ str ] | str , normalize : bool ) -> list [ str ] :
75
83
"""
76
84
Return a list of normalized locales that do not throw an ``Exception``
77
85
when set.
@@ -98,11 +106,15 @@ def _valid_locales(locales, normalize):
98
106
]
99
107
100
108
101
- def _default_locale_getter ():
109
+ def _default_locale_getter () -> bytes :
102
110
return subprocess .check_output (["locale -a" ], shell = True )
103
111
104
112
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 :
106
118
"""
107
119
Get all the locales that are available on the system.
108
120
@@ -142,9 +154,9 @@ def get_locales(prefix=None, normalize=True, locale_getter=_default_locale_gette
142
154
# raw_locales is "\n" separated list of locales
143
155
# it may contain non-decodable parts, so split
144
156
# extract what we can and then rejoin.
145
- raw_locales = raw_locales .split (b"\n " )
157
+ split_raw_locales = raw_locales .split (b"\n " )
146
158
out_locales = []
147
- for x in raw_locales :
159
+ for x in split_raw_locales :
148
160
try :
149
161
out_locales .append (str (x , encoding = options .display .encoding ))
150
162
except UnicodeError :
0 commit comments