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

babel.dates.format_timedelta throws KeyError exception on certain locale #818

Closed
khanguyen-yousician opened this issue Oct 20, 2021 · 2 comments

Comments

@khanguyen-yousician
Copy link

Overview Description

babel.dates.format_timedelta throws KeyError exception on certain locale. Version 2.7.0

Steps to Reproduce

from datetime import timedelta
import babel
from babel.core import Locale
from babel.dates import format_timedelta


def try_format(locale):
    try:
        format_timedelta(
            timedelta(hours=1),
            granularity="hour",
            threshold=float("inf"),
            format="narrow",
            locale=locale,
        )
        return True
    except Exception as e:
        print('%s => %s: %s' % (locale, e.__class__.__name__, e))
    return False


from babel.localedata import locale_identifiers

failed = 0
for locale in sorted(locale_identifiers()):
    if not try_format(locale):
        failed += 1

Actual Results

hr => KeyError: 'one'
hr_BA => KeyError: 'one'
hr_HR => KeyError: 'one'
pt => KeyError: 'one'
pt_BR => KeyError: 'one'

Expected Results

No KeyError exception.

@akx
Copy link
Member

akx commented Jan 25, 2022

Hi! The KeyError problem was fixed by #827, but not all of those locales have narrow data, so format_timedelta() would return an empty string.

@akx
Copy link
Member

akx commented May 10, 2022

With #827 merged (i.e. with Babel 3.10), the repro script doesn't raise exceptions. Thank you for the report!

@akx akx closed this as completed May 10, 2022
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