python-babel / babel

The official repository for Babel, the Python Internationalization Library
http://babel.pocoo.org/
BSD 3-Clause "New" or "Revised" License
1.29k stars 432 forks source link

Unexpected behaviour: parse_numbers() doesn't handle space as a grouping symbol, however, parse_decimal() does #1061

Open donnillo opened 5 months ago

donnillo commented 5 months ago

Overview Description

Function parse_decimal() from babel.numbers module is wise enough to handle spaces as a grouping symbol where non-breakable space \xa0 is expected. However, parse_number() does not reproduce the same logic.

Steps to Reproduce

from babel.numbers import (
    parse_number,
    parse_decimal
)

parse_decimal('1 099', locale='ru', numbering_system='default')  # OK
Decimal('1099')
parse_number('1 099', locale='ru', numbering_system='default')  # FAILS UNEXPECTEDLY
Traceback (most recent call last):
  File "/home/myuser/.pyenv/versions/3.11.2/lib/python3.11/site-packages/babel/numbers.py", line 1030, in parse_number
    return int(string.replace(get_group_symbol(locale, numbering_system=numbering_system), ''))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: invalid literal for int() with base 10: '1 099'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/myuser/.pyenv/versions/3.11.2/lib/python3.11/site-packages/babel/numbers.py", line 1032, in parse_number
    raise NumberFormatError(f"{string!r} is not a valid number") from ve
babel.numbers.NumberFormatError: '1 099' is not a valid number

Actual Results

parse_number() unreasonably fails while trying to parse number with spaces in locales where non-breakable space is used as a grouping symbol.

Expected Results

Space and non-breakable space should be considered the same while parsing numbers. Logic used in parse_decimal() function should be used in parse_number() as well.

parse_number('1 099', locale='ru', numbering_system='default')
1099

Reproducibility

Python 3.11.2 WSL\Ubuntu Babel 2.14.0

Additional Information

I guess, parse_number() should also introduce strict argument as well as parse_decimal() does.