Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
donnillo opened this issue Jan 25, 2024 · 0 comments

Comments

@donnillo
Copy link

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.

@donnillo donnillo changed the title Unexpected behaviour: parse_numbers() doesn't handle spaces as a grouping symbol, however, parse_decimal() does Unexpected behaviour: parse_numbers() doesn't handle space as a grouping symbol, however, parse_decimal() does Jan 25, 2024
sebas-inf added a commit to sebas-inf/babel that referenced this issue Jan 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants