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

Make LocaleDataDict Generic #961

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
104 changes: 72 additions & 32 deletions babel/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
if TYPE_CHECKING:
from typing_extensions import Literal, TypeAlias

from babel import dates, numbers

_GLOBAL_KEY: TypeAlias = Literal[
"all_currencies",
"currency_fractions",
Expand Down Expand Up @@ -423,7 +425,7 @@ def __str__(self) -> str:
self.modifier))

@property
def _data(self) -> localedata.LocaleDataDict:
def _data(self) -> localedata.LocaleDataDict[str, Any]:
if self.__data is None:
self.__data = localedata.LocaleDataDict(localedata.load(str(self)))
return self.__data
Expand Down Expand Up @@ -541,7 +543,7 @@ def english_name(self) -> str | None:
# { General Locale Display Names

@property
def languages(self) -> localedata.LocaleDataDict:
def languages(self) -> localedata.LocaleDataDict[str, str]:
"""Mapping of language codes to translated language names.

>>> Locale('de', 'DE').languages['ja']
Expand All @@ -553,7 +555,7 @@ def languages(self) -> localedata.LocaleDataDict:
return self._data['languages']

@property
def scripts(self) -> localedata.LocaleDataDict:
def scripts(self) -> localedata.LocaleDataDict[str, str]:
"""Mapping of script codes to translated script names.

>>> Locale('en', 'US').scripts['Hira']
Expand All @@ -565,7 +567,7 @@ def scripts(self) -> localedata.LocaleDataDict:
return self._data['scripts']

@property
def territories(self) -> localedata.LocaleDataDict:
def territories(self) -> localedata.LocaleDataDict[str, str]:
"""Mapping of script codes to translated script names.

>>> Locale('es', 'CO').territories['DE']
Expand All @@ -577,7 +579,7 @@ def territories(self) -> localedata.LocaleDataDict:
return self._data['territories']

@property
def variants(self) -> localedata.LocaleDataDict:
def variants(self) -> localedata.LocaleDataDict[str, str]:
"""Mapping of script codes to translated script names.

>>> Locale('de', 'DE').variants['1901']
Expand All @@ -588,7 +590,7 @@ def variants(self) -> localedata.LocaleDataDict:
# { Number Formatting

@property
def currencies(self) -> localedata.LocaleDataDict:
def currencies(self) -> localedata.LocaleDataDict[str, str]:
"""Mapping of currency codes to translated currency names. This
only returns the generic form of the currency name, not the count
specific one. If an actual number is requested use the
Expand All @@ -602,7 +604,7 @@ def currencies(self) -> localedata.LocaleDataDict:
return self._data['currency_names']

@property
def currency_symbols(self) -> localedata.LocaleDataDict:
def currency_symbols(self) -> localedata.LocaleDataDict[str, str]:
"""Mapping of currency codes to symbols.

>>> Locale('en', 'US').currency_symbols['USD']
Expand All @@ -613,7 +615,7 @@ def currency_symbols(self) -> localedata.LocaleDataDict:
return self._data['currency_symbols']

@property
def number_symbols(self) -> localedata.LocaleDataDict:
def number_symbols(self) -> localedata.LocaleDataDict[str, str]:
"""Symbols used in number formatting.

.. note:: The format of the value returned may change between
Expand All @@ -625,7 +627,7 @@ def number_symbols(self) -> localedata.LocaleDataDict:
return self._data['number_symbols']

@property
def decimal_formats(self) -> localedata.LocaleDataDict:
def decimal_formats(self) -> localedata.LocaleDataDict[str | None, numbers.NumberPattern]:
"""Locale patterns for decimal number formatting.

.. note:: The format of the value returned may change between
Expand All @@ -637,7 +639,11 @@ def decimal_formats(self) -> localedata.LocaleDataDict:
return self._data['decimal_formats']

@property
def compact_decimal_formats(self) -> localedata.LocaleDataDict:
def compact_decimal_formats(
self
) -> localedata.LocaleDataDict[
str, localedata.LocaleDataDict[str, localedata.LocaleDataDict[str, numbers.NumberPattern]]
]:
"""Locale patterns for compact decimal number formatting.

.. note:: The format of the value returned may change between
Expand All @@ -649,7 +655,7 @@ def compact_decimal_formats(self) -> localedata.LocaleDataDict:
return self._data['compact_decimal_formats']

@property
def currency_formats(self) -> localedata.LocaleDataDict:
def currency_formats(self) -> localedata.LocaleDataDict[str, numbers.NumberPattern]:
"""Locale patterns for currency number formatting.

.. note:: The format of the value returned may change between
Expand All @@ -663,7 +669,11 @@ def currency_formats(self) -> localedata.LocaleDataDict:
return self._data['currency_formats']

@property
def compact_currency_formats(self) -> localedata.LocaleDataDict:
def compact_currency_formats(
self
) -> localedata.LocaleDataDict[
str, localedata.LocaleDataDict[str, localedata.LocaleDataDict[str, numbers.NumberPattern]]
]:
"""Locale patterns for compact currency number formatting.

.. note:: The format of the value returned may change between
Expand All @@ -675,7 +685,7 @@ def compact_currency_formats(self) -> localedata.LocaleDataDict:
return self._data['compact_currency_formats']

@property
def percent_formats(self) -> localedata.LocaleDataDict:
def percent_formats(self) -> localedata.LocaleDataDict[str | None, numbers.NumberPattern]:
"""Locale patterns for percent number formatting.

.. note:: The format of the value returned may change between
Expand All @@ -687,7 +697,7 @@ def percent_formats(self) -> localedata.LocaleDataDict:
return self._data['percent_formats']

@property
def scientific_formats(self) -> localedata.LocaleDataDict:
def scientific_formats(self) -> localedata.LocaleDataDict[str | None, numbers.NumberPattern]:
"""Locale patterns for scientific number formatting.

.. note:: The format of the value returned may change between
Expand All @@ -701,7 +711,7 @@ def scientific_formats(self) -> localedata.LocaleDataDict:
# { Calendar Information and Date Formatting

@property
def periods(self) -> localedata.LocaleDataDict:
def periods(self) -> localedata.LocaleDataDict[str, str]:
"""Locale display names for day periods (AM/PM).

>>> Locale('en', 'US').periods['am']
Expand All @@ -713,21 +723,31 @@ def periods(self) -> localedata.LocaleDataDict:
return localedata.LocaleDataDict({}) # pragma: no cover

@property
def day_periods(self) -> localedata.LocaleDataDict:
def day_periods(
self
) -> localedata.LocaleDataDict[
str, localedata.LocaleDataDict[str, localedata.LocaleDataDict[str, str]]
]:
"""Locale display names for various day periods (not necessarily only AM/PM).

These are not meant to be used without the relevant `day_period_rules`.
"""
return self._data['day_periods']

@property
def day_period_rules(self) -> localedata.LocaleDataDict:
def day_period_rules(
self
) -> localedata.LocaleDataDict[str | None, localedata.LocaleDataDict[str, str]]:
"""Day period rules for the locale. Used by `get_period_id`.
"""
return self._data.get('day_period_rules', localedata.LocaleDataDict({}))

@property
def days(self) -> localedata.LocaleDataDict:
def days(
self
) -> localedata.LocaleDataDict[
str, localedata.LocaleDataDict[str, localedata.LocaleDataDict[int, str]]
]:
"""Locale display names for weekdays.

>>> Locale('de', 'DE').days['format']['wide'][3]
Expand All @@ -736,7 +756,11 @@ def days(self) -> localedata.LocaleDataDict:
return self._data['days']

@property
def months(self) -> localedata.LocaleDataDict:
def months(
self
) -> localedata.LocaleDataDict[
str, localedata.LocaleDataDict[str, localedata.LocaleDataDict[int, str]]
]:
"""Locale display names for months.

>>> Locale('de', 'DE').months['format']['wide'][10]
Expand All @@ -745,7 +769,11 @@ def months(self) -> localedata.LocaleDataDict:
return self._data['months']

@property
def quarters(self) -> localedata.LocaleDataDict:
def quarters(
self
) -> localedata.LocaleDataDict[
str, localedata.LocaleDataDict[str, localedata.LocaleDataDict[int, str]]
]:
"""Locale display names for quarters.

>>> Locale('de', 'DE').quarters['format']['wide'][1]
Expand All @@ -754,7 +782,7 @@ def quarters(self) -> localedata.LocaleDataDict:
return self._data['quarters']

@property
def eras(self) -> localedata.LocaleDataDict:
def eras(self) -> localedata.LocaleDataDict[str, localedata.LocaleDataDict[int, str]]:
"""Locale display names for eras.

.. note:: The format of the value returned may change between
Expand All @@ -768,7 +796,11 @@ def eras(self) -> localedata.LocaleDataDict:
return self._data['eras']

@property
def time_zones(self) -> localedata.LocaleDataDict:
def time_zones(
self
) -> localedata.LocaleDataDict[
str, localedata.LocaleDataDict[str, localedata.LocaleDataDict[str, str]]
]:
"""Locale display names for time zones.

.. note:: The format of the value returned may change between
Expand All @@ -782,7 +814,11 @@ def time_zones(self) -> localedata.LocaleDataDict:
return self._data['time_zones']

@property
def meta_zones(self) -> localedata.LocaleDataDict:
def meta_zones(
self
) -> localedata.LocaleDataDict[
str, localedata.LocaleDataDict[str, localedata.LocaleDataDict[str, str]]
]:
"""Locale display names for meta time zones.

Meta time zones are basically groups of different Olson time zones that
Expand All @@ -799,7 +835,7 @@ def meta_zones(self) -> localedata.LocaleDataDict:
return self._data['meta_zones']

@property
def zone_formats(self) -> localedata.LocaleDataDict:
def zone_formats(self) -> localedata.LocaleDataDict[str, str]:
"""Patterns related to the formatting of time zones.

.. note:: The format of the value returned may change between
Expand Down Expand Up @@ -854,7 +890,7 @@ def min_week_days(self) -> int:
return self._data['week_data']['min_days']

@property
def date_formats(self) -> localedata.LocaleDataDict:
def date_formats(self) -> localedata.LocaleDataDict[str, dates.DateTimePattern]:
"""Locale patterns for date formatting.

.. note:: The format of the value returned may change between
Expand All @@ -868,7 +904,7 @@ def date_formats(self) -> localedata.LocaleDataDict:
return self._data['date_formats']

@property
def time_formats(self) -> localedata.LocaleDataDict:
def time_formats(self) -> localedata.LocaleDataDict[str, dates.DateTimePattern]:
"""Locale patterns for time formatting.

.. note:: The format of the value returned may change between
Expand All @@ -882,7 +918,7 @@ def time_formats(self) -> localedata.LocaleDataDict:
return self._data['time_formats']

@property
def datetime_formats(self) -> localedata.LocaleDataDict:
def datetime_formats(self) -> localedata.LocaleDataDict[str, str]:
"""Locale patterns for datetime formatting.

.. note:: The format of the value returned may change between
Expand All @@ -896,7 +932,7 @@ def datetime_formats(self) -> localedata.LocaleDataDict:
return self._data['datetime_formats']

@property
def datetime_skeletons(self) -> localedata.LocaleDataDict:
def datetime_skeletons(self) -> localedata.LocaleDataDict[str, dates.DateTimePattern]:
"""Locale patterns for formatting parts of a datetime.

>>> Locale('en').datetime_skeletons['MEd']
Expand All @@ -909,7 +945,9 @@ def datetime_skeletons(self) -> localedata.LocaleDataDict:
return self._data['datetime_skeletons']

@property
def interval_formats(self) -> localedata.LocaleDataDict:
def interval_formats(
self
) -> localedata.LocaleDataDict[str | None, str | localedata.LocaleDataDict[str, list[str]]]:
"""Locale patterns for interval formatting.

.. note:: The format of the value returned may change between
Expand Down Expand Up @@ -946,7 +984,7 @@ def plural_form(self) -> PluralRule:
return self._data.get('plural_form', _default_plural_rule)

@property
def list_patterns(self) -> localedata.LocaleDataDict:
def list_patterns(self) -> localedata.LocaleDataDict[str, localedata.LocaleDataDict[str, str]]:
"""Patterns for generating lists

.. note:: The format of the value returned may change between
Expand Down Expand Up @@ -979,7 +1017,7 @@ def ordinal_form(self) -> PluralRule:
return self._data.get('ordinal_form', _default_plural_rule)

@property
def measurement_systems(self) -> localedata.LocaleDataDict:
def measurement_systems(self) -> localedata.LocaleDataDict[str, str]:
"""Localized names for various measurement systems.

>>> Locale('fr', 'FR').measurement_systems['US']
Expand Down Expand Up @@ -1013,7 +1051,9 @@ def text_direction(self) -> str:
return ''.join(word[0] for word in self.character_order.split('-'))

@property
def unit_display_names(self) -> localedata.LocaleDataDict:
def unit_display_names(
self
) -> localedata.LocaleDataDict[str, localedata.LocaleDataDict[str, str]]:
"""Display names for units of measurement.

.. seealso::
Expand Down
Loading