I noticed that the Locale class under babel/core.py uses str(...) when getting it's identifiers.
This can pose an issue for overriding classes that wish to modify the str representation of the object.
Looking at the class, all other points in the class (and str dunder itself) use a different function (get_locale_identifier), that can be used in init too, and free str to modifications. As of right now modifying it causes a bug on creating the Locale object properly and limiting usage.
def__init__(
self,
language: str,
territory: str|None=None,
script: str|None=None,
variant: str|None=None,
modifier: str|None=None,
) ->None:
"""Initialize the locale object from the given identifier components. >>> locale = Locale('en', 'US') >>> locale.language 'en' >>> locale.territory 'US' :param language: the language code :param territory: the territory (country or region) code :param script: the script code :param variant: the variant code :param modifier: a modifier (following the '@' symbol, sometimes called '@variant') :raise `UnknownLocaleError`: if no locale data is available for the requested locale """#: the language codeself.language=language#: the territory (country or region) codeself.territory=territory#: the script codeself.script=script#: the variant codeself.variant=variant#: the modifierself.modifier=modifierself.__data: localedata.LocaleDataDict|None=None# identifier = str(this) <-- this is the old part, the following is the proposed solution to free __str__ up for inheriting classesidentifier=get_locale_identifier(
(self.language, self.territory, self.script, self.variant, self.modifier),
)
identifier_without_modifier=identifier.partition('@')[0]
iflocaledata.exists(identifier):
self.__data_identifier=identifiereliflocaledata.exists(identifier_without_modifier):
self.__data_identifier=identifier_without_modifierelse:
raiseUnknownLocaleError(identifier)
# Current implementation of str in the Locale classdef__str__(self) ->str:
returnget_locale_identifier(
(self.language, self.territory, self.script, self.variant, self.modifier),
)
Hi, i'm working on it as part of university assignment where i need to contribute to an open-source project. So, i just wanted to check if it's okay for me to work on it. Thanks
Activity
Chiifouu commented on Mar 4, 2026
Hi, i'm working on it as part of university assignment where i need to contribute to an open-source project. So, i just wanted to check if it's okay for me to work on it. Thanks
Chiifouu commented on Jun 3, 2026
Hi, i have fixed the issue . Could you please review my PR and validate it if everything looks good ? Let me know if anything is missing. Thanks !