summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAarni Koskela <akx@iki.fi>2023-03-01 20:04:18 +0200
committerGitHub <noreply@github.com>2023-03-01 20:04:18 +0200
commit0ce196fccc024b1a65453ba6519954ada1dab6cb (patch)
treea8dd1451de5cd11ba317f5a0052161ab009dde4a
parent0aa54caa0ac47a1bbb5510812d9939344a8f16bb (diff)
parent69aafef0fdc3ea346227ba7133c3625be8cde68a (diff)
downloadbabel-0ce196fccc024b1a65453ba6519954ada1dab6cb.tar.gz
Merge pull request #981 from python-babel/mypy-misc-fix
Misc. mypy-discovered fixes
-rw-r--r--babel/core.py10
-rw-r--r--babel/messages/jslexer.py2
-rw-r--r--babel/numbers.py14
-rw-r--r--babel/units.py23
4 files changed, 27 insertions, 22 deletions
diff --git a/babel/core.py b/babel/core.py
index ba8a621..f63b97b 100644
--- a/babel/core.py
+++ b/babel/core.py
@@ -197,7 +197,7 @@ class Locale:
self.variant = variant
#: the modifier
self.modifier = modifier
- self.__data = None
+ self.__data: localedata.LocaleDataDict | None = None
identifier = str(self)
identifier_without_modifier = identifier.partition('@')[0]
@@ -260,6 +260,7 @@ class Locale:
aliases=aliases)
if identifier:
return Locale.parse(identifier, sep=sep)
+ return None
@classmethod
def parse(
@@ -468,9 +469,9 @@ class Locale:
details.append(locale.variants.get(self.variant))
if self.modifier:
details.append(self.modifier)
- details = filter(None, details)
- if details:
- retval += f" ({', '.join(details)})"
+ detail_string = ', '.join(atom for atom in details if atom)
+ if detail_string:
+ retval += f" ({detail_string})"
return retval
display_name = property(get_display_name, doc="""\
@@ -1080,6 +1081,7 @@ def default_locale(category: str | None = None, aliases: Mapping[str, str] = LOC
return get_locale_identifier(parse_locale(locale))
except ValueError:
pass
+ return None
def negotiate_locale(preferred: Iterable[str], available: Iterable[str], sep: str = '_', aliases: Mapping[str, str] = LOCALE_ALIASES) -> str | None:
diff --git a/babel/messages/jslexer.py b/babel/messages/jslexer.py
index d3389d0..cf287e4 100644
--- a/babel/messages/jslexer.py
+++ b/babel/messages/jslexer.py
@@ -98,7 +98,7 @@ def unquote_string(string: str) -> str:
assert string and string[0] == string[-1] and string[0] in '"\'`', \
'string provided is not properly delimited'
string = line_join_re.sub('\\1', string[1:-1])
- result = []
+ result: list[str] = []
add = result.append
pos = 0
diff --git a/babel/numbers.py b/babel/numbers.py
index de0f419..37b2bef 100644
--- a/babel/numbers.py
+++ b/babel/numbers.py
@@ -60,10 +60,8 @@ def list_currencies(locale: Locale | str | None = None) -> set[str]:
"""
# Get locale-scoped currencies.
if locale:
- currencies = Locale.parse(locale).currencies.keys()
- else:
- currencies = get_global('all_currencies')
- return set(currencies)
+ return set(Locale.parse(locale).currencies)
+ return set(get_global('all_currencies'))
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
if isinstance(currency, str):
currency = currency.upper()
if not is_currency(currency, locale):
- return
+ return None
return currency
@@ -706,7 +704,7 @@ def _format_currency_long_name(
# Step 5.
if not format:
- format = locale.decimal_formats[format]
+ format = locale.decimal_formats[None]
pattern = parse_pattern(format)
@@ -810,7 +808,7 @@ def format_percent(
"""
locale = Locale.parse(locale)
if not format:
- format = locale.percent_formats[format]
+ format = locale.percent_formats[None]
pattern = parse_pattern(format)
return pattern.apply(
number, locale, decimal_quantization=decimal_quantization, group_separator=group_separator)
@@ -849,7 +847,7 @@ def format_scientific(
"""
locale = Locale.parse(locale)
if not format:
- format = locale.scientific_formats[format]
+ format = locale.scientific_formats[None]
pattern = parse_pattern(format)
return pattern.apply(
number, locale, decimal_quantization=decimal_quantization)
diff --git a/babel/units.py b/babel/units.py
index 0c72ee9..2da1a8a 100644
--- a/babel/units.py
+++ b/babel/units.py
@@ -72,10 +72,11 @@ def _find_unit_pattern(unit_id: str, locale: Locale | str | None = LC_NUMERIC) -
for unit_pattern in sorted(unit_patterns, key=len):
if unit_pattern.endswith(unit_id):
return unit_pattern
+ return None
def format_unit(
- value: float | decimal.Decimal,
+ value: str | float | decimal.Decimal,
measurement_unit: str,
length: Literal['short', 'long', 'narrow'] = 'long',
format: str | None = None,
@@ -184,18 +185,18 @@ def _find_compound_unit(
# units like "kilometer" or "hour" into actual units like "length-kilometer" and
# "duration-hour".
- numerator_unit = _find_unit_pattern(numerator_unit, locale=locale)
- denominator_unit = _find_unit_pattern(denominator_unit, locale=locale)
+ resolved_numerator_unit = _find_unit_pattern(numerator_unit, locale=locale)
+ resolved_denominator_unit = _find_unit_pattern(denominator_unit, locale=locale)
# If either was not found, we can't possibly build a suitable compound unit either.
- if not (numerator_unit and denominator_unit):
+ if not (resolved_numerator_unit and resolved_denominator_unit):
return None
# Since compound units are named "speed-kilometer-per-hour", we'll have to slice off
# the quantities (i.e. "length", "duration") from both qualified units.
- bare_numerator_unit = numerator_unit.split("-", 1)[-1]
- bare_denominator_unit = denominator_unit.split("-", 1)[-1]
+ bare_numerator_unit = resolved_numerator_unit.split("-", 1)[-1]
+ bare_denominator_unit = resolved_denominator_unit.split("-", 1)[-1]
# Now we can try and rebuild a compound unit specifier, then qualify it:
@@ -203,9 +204,9 @@ def _find_compound_unit(
def format_compound_unit(
- numerator_value: float | decimal.Decimal,
+ numerator_value: str | float | decimal.Decimal,
numerator_unit: str | None = None,
- denominator_value: float | decimal.Decimal = 1,
+ denominator_value: str | float | decimal.Decimal = 1,
denominator_unit: str | None = None,
length: Literal["short", "long", "narrow"] = "long",
format: str | None = None,
@@ -289,7 +290,11 @@ def format_compound_unit(
denominator_value = ""
formatted_denominator = format_unit(
- denominator_value, denominator_unit, length=length, format=format, locale=locale
+ denominator_value,
+ measurement_unit=(denominator_unit or ""),
+ length=length,
+ format=format,
+ locale=locale,
).strip()
else: # Bare denominator
formatted_denominator = format_decimal(denominator_value, format=format, locale=locale)