Skip to content

Commit 0ce196f

Browse files
authored
Merge pull request #981 from python-babel/mypy-misc-fix
Misc. mypy-discovered fixes
2 parents 0aa54ca + 69aafef commit 0ce196f

File tree

4 files changed

+27
-22
lines changed

4 files changed

+27
-22
lines changed

babel/core.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def __init__(
197197
self.variant = variant
198198
#: the modifier
199199
self.modifier = modifier
200-
self.__data = None
200+
self.__data: localedata.LocaleDataDict | None = None
201201

202202
identifier = str(self)
203203
identifier_without_modifier = identifier.partition('@')[0]
@@ -260,6 +260,7 @@ def negotiate(
260260
aliases=aliases)
261261
if identifier:
262262
return Locale.parse(identifier, sep=sep)
263+
return None
263264

264265
@classmethod
265266
def parse(
@@ -468,9 +469,9 @@ def get_display_name(self, locale: Locale | str | None = None) -> str | None:
468469
details.append(locale.variants.get(self.variant))
469470
if self.modifier:
470471
details.append(self.modifier)
471-
details = filter(None, details)
472-
if details:
473-
retval += f" ({', '.join(details)})"
472+
detail_string = ', '.join(atom for atom in details if atom)
473+
if detail_string:
474+
retval += f" ({detail_string})"
474475
return retval
475476

476477
display_name = property(get_display_name, doc="""\
@@ -1080,6 +1081,7 @@ def default_locale(category: str | None = None, aliases: Mapping[str, str] = LOC
10801081
return get_locale_identifier(parse_locale(locale))
10811082
except ValueError:
10821083
pass
1084+
return None
10831085

10841086

10851087
def negotiate_locale(preferred: Iterable[str], available: Iterable[str], sep: str = '_', aliases: Mapping[str, str] = LOCALE_ALIASES) -> str | None:

babel/messages/jslexer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def unquote_string(string: str) -> str:
9898
assert string and string[0] == string[-1] and string[0] in '"\'`', \
9999
'string provided is not properly delimited'
100100
string = line_join_re.sub('\\1', string[1:-1])
101-
result = []
101+
result: list[str] = []
102102
add = result.append
103103
pos = 0
104104

babel/numbers.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,8 @@ def list_currencies(locale: Locale | str | None = None) -> set[str]:
6060
"""
6161
# Get locale-scoped currencies.
6262
if locale:
63-
currencies = Locale.parse(locale).currencies.keys()
64-
else:
65-
currencies = get_global('all_currencies')
66-
return set(currencies)
63+
return set(Locale.parse(locale).currencies)
64+
return set(get_global('all_currencies'))
6765

6866

6967
def validate_currency(currency: str, locale: Locale | str | None = None) -> None:
@@ -103,7 +101,7 @@ def normalize_currency(currency: str, locale: Locale | str | None = None) -> str
103101
if isinstance(currency, str):
104102
currency = currency.upper()
105103
if not is_currency(currency, locale):
106-
return
104+
return None
107105
return currency
108106

109107

@@ -706,7 +704,7 @@ def _format_currency_long_name(
706704

707705
# Step 5.
708706
if not format:
709-
format = locale.decimal_formats[format]
707+
format = locale.decimal_formats[None]
710708

711709
pattern = parse_pattern(format)
712710

@@ -810,7 +808,7 @@ def format_percent(
810808
"""
811809
locale = Locale.parse(locale)
812810
if not format:
813-
format = locale.percent_formats[format]
811+
format = locale.percent_formats[None]
814812
pattern = parse_pattern(format)
815813
return pattern.apply(
816814
number, locale, decimal_quantization=decimal_quantization, group_separator=group_separator)
@@ -849,7 +847,7 @@ def format_scientific(
849847
"""
850848
locale = Locale.parse(locale)
851849
if not format:
852-
format = locale.scientific_formats[format]
850+
format = locale.scientific_formats[None]
853851
pattern = parse_pattern(format)
854852
return pattern.apply(
855853
number, locale, decimal_quantization=decimal_quantization)

babel/units.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ def _find_unit_pattern(unit_id: str, locale: Locale | str | None = LC_NUMERIC) -
7272
for unit_pattern in sorted(unit_patterns, key=len):
7373
if unit_pattern.endswith(unit_id):
7474
return unit_pattern
75+
return None
7576

7677

7778
def format_unit(
78-
value: float | decimal.Decimal,
79+
value: str | float | decimal.Decimal,
7980
measurement_unit: str,
8081
length: Literal['short', 'long', 'narrow'] = 'long',
8182
format: str | None = None,
@@ -184,28 +185,28 @@ def _find_compound_unit(
184185
# units like "kilometer" or "hour" into actual units like "length-kilometer" and
185186
# "duration-hour".
186187

187-
numerator_unit = _find_unit_pattern(numerator_unit, locale=locale)
188-
denominator_unit = _find_unit_pattern(denominator_unit, locale=locale)
188+
resolved_numerator_unit = _find_unit_pattern(numerator_unit, locale=locale)
189+
resolved_denominator_unit = _find_unit_pattern(denominator_unit, locale=locale)
189190

190191
# If either was not found, we can't possibly build a suitable compound unit either.
191-
if not (numerator_unit and denominator_unit):
192+
if not (resolved_numerator_unit and resolved_denominator_unit):
192193
return None
193194

194195
# Since compound units are named "speed-kilometer-per-hour", we'll have to slice off
195196
# the quantities (i.e. "length", "duration") from both qualified units.
196197

197-
bare_numerator_unit = numerator_unit.split("-", 1)[-1]
198-
bare_denominator_unit = denominator_unit.split("-", 1)[-1]
198+
bare_numerator_unit = resolved_numerator_unit.split("-", 1)[-1]
199+
bare_denominator_unit = resolved_denominator_unit.split("-", 1)[-1]
199200

200201
# Now we can try and rebuild a compound unit specifier, then qualify it:
201202

202203
return _find_unit_pattern(f"{bare_numerator_unit}-per-{bare_denominator_unit}", locale=locale)
203204

204205

205206
def format_compound_unit(
206-
numerator_value: float | decimal.Decimal,
207+
numerator_value: str | float | decimal.Decimal,
207208
numerator_unit: str | None = None,
208-
denominator_value: float | decimal.Decimal = 1,
209+
denominator_value: str | float | decimal.Decimal = 1,
209210
denominator_unit: str | None = None,
210211
length: Literal["short", "long", "narrow"] = "long",
211212
format: str | None = None,
@@ -289,7 +290,11 @@ def format_compound_unit(
289290
denominator_value = ""
290291

291292
formatted_denominator = format_unit(
292-
denominator_value, denominator_unit, length=length, format=format, locale=locale
293+
denominator_value,
294+
measurement_unit=(denominator_unit or ""),
295+
length=length,
296+
format=format,
297+
locale=locale,
293298
).strip()
294299
else: # Bare denominator
295300
formatted_denominator = format_decimal(denominator_value, format=format, locale=locale)

0 commit comments

Comments
 (0)