import os
import pickle
from collections.abc import Iterable, Mapping
-from typing import TYPE_CHECKING, Any, overload
+from typing import TYPE_CHECKING, Any
from babel import localedata
from babel.plural import PluralRule
if identifier:
return Locale.parse(identifier, sep=sep)
- @overload
- @classmethod
- def parse(cls, identifier: None, sep: str = ..., resolve_likely_subtags: bool = ...) -> None: ...
-
- @overload
- @classmethod
- def parse(cls, identifier: str | Locale, sep: str = ..., resolve_likely_subtags: bool = ...) -> Locale: ...
-
@classmethod
def parse(
cls,
identifier: str | Locale | None,
sep: str = '_',
resolve_likely_subtags: bool = True,
- ) -> Locale | None:
+ ) -> Locale:
"""Create a `Locale` instance for the given locale identifier.
>>> l = Locale.parse('de-DE', sep='-')
identifier
:raise `UnknownLocaleError`: if no locale data is available for the
requested locale
+ :raise `TypeError`: if the identifier is not a string or a `Locale`
"""
- if identifier is None:
- return None
- elif isinstance(identifier, Locale):
+ if isinstance(identifier, Locale):
return identifier
elif not isinstance(identifier, str):
raise TypeError(f"Unexpected value for identifier: {identifier!r}")
language, territory, script, variant = parts
modifier = None
language = get_global('language_aliases').get(language, language)
- territory = get_global('territory_aliases').get(territory, (territory,))[0]
- script = get_global('script_aliases').get(script, script)
- variant = get_global('variant_aliases').get(variant, variant)
+ territory = get_global('territory_aliases').get(territory or '', (territory,))[0]
+ script = get_global('script_aliases').get(script or '', script)
+ variant = get_global('variant_aliases').get(variant or '', variant)
if territory == 'ZZ':
territory = None
if likely_subtag is not None:
parts2 = parse_locale(likely_subtag)
if len(parts2) == 5:
- language2, _, script2, variant2, modifier2 = parse_locale(likely_subtag)
+ language2, _, script2, variant2, modifier2 = parts2
else:
- language2, _, script2, variant2 = parse_locale(likely_subtag)
+ language2, _, script2, variant2 = parts2
modifier2 = None
locale = _try_load_reducing((language2, territory, script2, variant2, modifier2))
if locale is not None:
def parse_locale(
identifier: str,
sep: str = '_'
-) -> tuple[str, str | None, str | None, str | None, str | None]:
+) -> tuple[str, str | None, str | None, str | None] | tuple[str, str | None, str | None, str | None, str | None]:
"""Parse a locale identifier into a tuple of the form ``(language,
territory, script, variant, modifier)``.
:param tup: the tuple as returned by :func:`parse_locale`.
:param sep: the separator for the identifier.
"""
- tup = tuple(tup[:5])
+ tup = tuple(tup[:5]) # type: ignore # length should be no more than 5
lang, territory, script, variant, modifier = tup + (None,) * (5 - len(tup))
ret = sep.join(filter(None, (lang, script, territory, variant)))
return f'{ret}@{modifier}' if modifier else ret
import decimal
import re
import warnings
-from typing import TYPE_CHECKING, Any, overload
+from typing import TYPE_CHECKING, Any, cast, overload
from babel.core import Locale, default_locale, get_global
from babel.localedata import LocaleDataDict
def format_decimal(
number: float | decimal.Decimal | str,
- format: str | None = None,
+ format: str | NumberPattern | None = None,
locale: Locale | str | None = LC_NUMERIC,
decimal_quantization: bool = True,
group_separator: bool = True,
number format.
"""
locale = Locale.parse(locale)
- if not format:
- format = locale.decimal_formats.get(format)
+ if format is None:
+ format = locale.decimal_formats[format]
pattern = parse_pattern(format)
return pattern.apply(
number, locale, decimal_quantization=decimal_quantization, group_separator=group_separator)
number, format = _get_compact_format(number, compact_format, locale, fraction_digits)
# Did not find a format, fall back.
if format is None:
- format = locale.decimal_formats.get(None)
+ format = locale.decimal_formats[None]
pattern = parse_pattern(format)
return pattern.apply(number, locale, decimal_quantization=False)
def _get_compact_format(
number: float | decimal.Decimal | str,
compact_format: LocaleDataDict,
- locale: Locale | str | None,
+ locale: Locale,
fraction_digits: int,
) -> tuple[decimal.Decimal, NumberPattern | None]:
"""Returns the number after dividing by the unit and the format pattern to use.
break
# otherwise, we need to divide the number by the magnitude but remove zeros
# equal to the number of 0's in the pattern minus 1
- number = number / (magnitude // (10 ** (pattern.count("0") - 1)))
+ number = cast(decimal.Decimal, number / (magnitude // (10 ** (pattern.count("0") - 1))))
# round to the number of fraction digits requested
rounded = round(number, fraction_digits)
# if the remaining number is singular, use the singular format
def format_currency(
number: float | decimal.Decimal | str,
currency: str,
- format: str | None = None,
+ format: str | NumberPattern | None = None,
locale: Locale | str | None = LC_NUMERIC,
currency_digits: bool = True,
format_type: Literal["name", "standard", "accounting"] = "standard",
def _format_currency_long_name(
number: float | decimal.Decimal | str,
currency: str,
- format: str | None = None,
+ format: str | NumberPattern | None = None,
locale: Locale | str | None = LC_NUMERIC,
currency_digits: bool = True,
format_type: Literal["name", "standard", "accounting"] = "standard",
# Step 5.
if not format:
- format = locale.decimal_formats.get(format)
+ format = locale.decimal_formats[format]
pattern = parse_pattern(format)
# compress adjacent spaces into one
format = re.sub(r'(\s)\s+', r'\1', format).strip()
break
+ if format is None:
+ raise ValueError('No compact currency format found for the given number and locale.')
pattern = parse_pattern(format)
return pattern.apply(number, locale, currency=currency, currency_digits=False, decimal_quantization=False)
def format_percent(
number: float | decimal.Decimal | str,
- format: str | None = None,
+ format: str | NumberPattern | None = None,
locale: Locale | str | None = LC_NUMERIC,
decimal_quantization: bool = True,
group_separator: bool = True,
"""
locale = Locale.parse(locale)
if not format:
- format = locale.percent_formats.get(format)
+ format = locale.percent_formats[format]
pattern = parse_pattern(format)
return pattern.apply(
number, locale, decimal_quantization=decimal_quantization, group_separator=group_separator)
def format_scientific(
number: float | decimal.Decimal | str,
- format: str | None = None,
+ format: str | NumberPattern | None = None,
locale: Locale | str | None = LC_NUMERIC,
decimal_quantization: bool = True,
) -> str:
"""
locale = Locale.parse(locale)
if not format:
- format = locale.scientific_formats.get(format)
+ format = locale.scientific_formats[format]
pattern = parse_pattern(format)
return pattern.apply(
number, locale, decimal_quantization=decimal_quantization)
class NumberFormatError(ValueError):
"""Exception raised when a string cannot be parsed into a number."""
- def __init__(self, message: str, suggestions: str | None = None) -> None:
+ def __init__(self, message: str, suggestions: list[str] | None = None) -> None:
super().__init__(message)
#: a list of properly formatted numbers derived from the invalid input
self.suggestions = suggestions
def apply(
self,
- value: float | decimal.Decimal,
+ value: float | decimal.Decimal | str,
locale: Locale | str | None,
currency: str | None = None,
currency_digits: bool = True,
number = ''.join([
self._quantize_value(value, locale, frac_prec, group_separator),
get_exponential_symbol(locale),
- exp_sign,
- self._format_int(
- str(exp), self.exp_prec[0], self.exp_prec[1], locale)])
+ exp_sign, # type: ignore # exp_sign is always defined here
+ self._format_int(str(exp), self.exp_prec[0], self.exp_prec[1], locale) # type: ignore # exp is always defined here
+ ])
# Is it a significant digits pattern?
elif '@' in self.pattern:
number if self.number_pattern != '' else '',
self.suffix[is_negative]])
- if '¤' in retval:
- retval = retval.replace('¤¤¤',
- get_currency_name(currency, value, locale))
+ if '¤' in retval and currency is not None:
+ retval = retval.replace('¤¤¤', get_currency_name(currency, value, locale))
retval = retval.replace('¤¤', currency.upper())
retval = retval.replace('¤', get_currency_symbol(currency, locale))