Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed Nov 28, 2023
1 parent 648bddb commit abe6bda
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
28 changes: 20 additions & 8 deletions babel/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ def _get_numbering_system(locale: Locale, numbering_system: Literal["default"] |


def _get_number_symbols(
locale: Locale | str,
locale: Locale | str | None,
*,
numbering_system: Literal["default"] | str = "latn",
) -> LocaleDataDict:
Expand Down Expand Up @@ -1275,7 +1275,8 @@ def scientific_notation_elements(
self,
value: decimal.Decimal,
locale: Locale | str | None,
numbering_system: str,
*,
numbering_system: Literal["default"] | str = "latn",
) -> tuple[decimal.Decimal, int, str]:
""" Returns normalized scientific notation components of a value.
"""
Expand Down Expand Up @@ -1337,8 +1338,8 @@ def apply(
:type decimal_quantization: bool
:param force_frac: DEPRECATED - a forced override for `self.frac_prec`
for a single formatting invocation.
:param numbering_system: The numbering system used for formatting number symbols. Defaults to "latn", special
value "default" will use the default numbering system of the locale.
:param numbering_system: The numbering system used for formatting number symbols. Defaults to "latn".
The special value "default" will use the default numbering system of the locale.
:return: Formatted decimal string.
:rtype: str
:raise UnsupportedNumberingSystemError: If the numbering system is not supported by the locale.
Expand Down Expand Up @@ -1458,7 +1459,15 @@ def _format_significant(self, value: decimal.Decimal, minimum: int, maximum: int
).rstrip('.')
return result

def _format_int(self, value: str, min: int, max: int, locale: Locale | str | None, numbering_system: str) -> str:
def _format_int(
self,
value: str,
min: int,
max: int,
locale: Locale | str | None,
*,
numbering_system: Literal["default"] | str,
) -> str:
width = len(value)
if width < min:
value = '0' * (min - width) + value
Expand All @@ -1475,8 +1484,10 @@ def _quantize_value(
self,
value: decimal.Decimal,
locale: Locale | str | None,
frac_prec: tuple[int, int], group_separator: bool,
numbering_system: str,
frac_prec: tuple[int, int],
group_separator: bool,
*,
numbering_system: Literal["default"] | str,
) -> str:
# If the number is +/-Infinity, we can't quantize it
if value.is_infinite():
Expand All @@ -1493,9 +1504,10 @@ def _quantize_value(
def _format_frac(
self,
value: str,
numbering_system: str,
locale: Locale | str | None,
force_frac: tuple[int, int] | None = None,
*,
numbering_system: Literal["default"] | str,
) -> str:
min, max = force_frac or self.frac_prec
if len(value) < min:
Expand Down
2 changes: 1 addition & 1 deletion babel/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def __init__(
self,
locale: Locale | str,
tzinfo: datetime.tzinfo | None = None,
*,
numbering_system: Literal["default"] | str = "latn",
) -> None:
"""Initialize the formatter.
Expand All @@ -61,7 +62,6 @@ def __init__(
:param tzinfo: the time-zone info (a `tzinfo` instance or `None`)
:param numbering_system: The numbering system used for formatting number symbols. Defaults to "latn".
The special value "default" will use the default numbering system of the locale.
:raise `UnsupportedNumberingSystemError`: If the numbering system is not supported by the locale.
"""
self.locale = Locale.parse(locale)
self.tzinfo = tzinfo
Expand Down
2 changes: 1 addition & 1 deletion babel/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def format_compound_unit(
>>> format_compound_unit(32.5, "ton", 15, denominator_unit="hour", locale="en")
'32.5 tons per 15 hours'
>>> format_compound_unit(1234.5, "ton", 15, denominator_unit="hour", locale="ar_EG", numbering_system="default")
>>> format_compound_unit(1234.5, "ton", 15, denominator_unit="hour", locale="ar_EG", numbering_system="arab")
'1٬234٫5 طن لكل 15 ساعة'
>>> format_compound_unit(160, denominator_unit="square-meter", locale="fr")
Expand Down
4 changes: 0 additions & 4 deletions tests/test_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,6 @@ def test_format_decimal():
with pytest.raises(numbers.UnsupportedNumberingSystemError):
numbers.format_decimal(12345.5, locale='en_US', numbering_system="unknown")




@pytest.mark.parametrize('input_value, expected_value', [
('10000', '10,000'),
('1', '1'),
Expand Down Expand Up @@ -742,7 +739,6 @@ def test_parse_number():
with pytest.raises(numbers.UnsupportedNumberingSystemError):
numbers.parse_number('1.099,98', locale='en', numbering_system="unsupported")


def test_parse_decimal():
assert (numbers.parse_decimal('1,099.98', locale='en_US')
== decimal.Decimal('1099.98'))
Expand Down

0 comments on commit abe6bda

Please sign in to comment.