python-babel / babel

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

`locale` parameter cannot be `None` #1156

Open injust opened 1 week ago

injust commented 1 week ago

Overview Description

The type annotations for the locale parameter (e.g. in babel.numbers.format_currency() suggest that the value can be None, but this causes a TypeError in Locale.parse().

Steps to Reproduce

babel.numbers.format_currency(0, "USD", locale=None)

Actual Results

Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    babel.numbers.format_currency(0, "USD", locale=None)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.13/site-packages/babel/numbers.py", line 777, in format_currency
    locale = Locale.parse(locale)
  File ".venv/lib/python3.13/site-packages/babel/core.py", line 332, in parse
    raise TypeError(f"Unexpected value for identifier: {identifier!r}")
TypeError: Unexpected value for identifier: None

Expected Results

I would expect a value of None to use the default value, or None should be removed from the type annotations entirely.

Reproducibility

Additional Information

bestpantusen commented 16 hours ago

If possible, I'd like to work on this please.

tomasr8 commented 15 hours ago

If possible, I'd like to work on this please.

Go for it! Feel free to ask if you need help :)