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

`KeyError` raised in `format_skeleton` when using fuzzy matching #1084

Open tomasr8 opened 1 month ago

tomasr8 commented 1 month ago

Overview Description

When using format_skeleton, a KeyError is raised despite using fuzzy=True.

Steps to Reproduce

from datetime import datetime

from babel.dates import format_skeleton

dt = datetime(2012, 1, 1, 14, 30, 59)
format_skeleton("G", dt, locale="cs_CZ", fuzzy=True)

From the docs:

fuzzy – If the skeleton is not found, allow choosing a skeleton that’s close enough to it.

The way I read it is that as long as I pass fuzzy=True (which is the default) a skeleton should always be found and I should not need to worry about KeyErrors in that case.

Even the example in the docs makes it seem a KeyError is only thrown with fuzzy=False:

format_skeleton('yMMd', t, fuzzy=False, locale='fi')  # yMMd is not in the Finnish locale, an error is thrown

If this is the expected behaviour, I think the docs should state that explicitly. If not, I would suggest adding allow_different_fields=True to the underlying match_skeleton() call.

Additional Information

Babel version: 2.15