Add type annotations to Locale

Task-number: QTBUG-129564
Change-Id: I6fe00162251ffa56c86ba2af98b8f066c9b5f09b
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
(cherry picked from commit bb32299e75c1eb4c4125455d184dd633733b3fb0)
This commit is contained in:
Mate Barany 2024-10-28 17:19:51 +01:00
parent fd27727e26
commit 4f2c976b7e

View File

@ -495,18 +495,19 @@ class Locale (object):
same signatures as those of a dict, acting on the instance's
__dict__, so the results are accessed as attributes rather than
mapping keys."""
def __init__(self, data=None, **kw):
def __init__(self, data: dict[str, Any]|None = None, **kw: Any) -> None:
self.update(data, **kw)
def update(self, data=None, **kw):
def update(self, data: dict[str, Any]|None = None, **kw: Any) -> None:
if data: self.__dict__.update(data)
if kw: self.__dict__.update(kw)
def __len__(self): # Used when testing as a boolean
def __len__(self) -> int: # Used when testing as a boolean
return len(self.__dict__)
@staticmethod
def propsMonthDay(scale, lengths=('long', 'short', 'narrow')):
def propsMonthDay(scale: str, lengths: tuple[str, str, str] = ('long', 'short', 'narrow')
) -> Iterator[str]:
for L in lengths:
yield camelCase((L, scale))
yield camelCase(('standalone', L, scale))
@ -534,7 +535,8 @@ class Locale (object):
__qDoW = {"mon": 1, "tue": 2, "wed": 3, "thu": 4, "fri": 5, "sat": 6, "sun": 7}
@classmethod
def fromXmlData(cls, lookup, calendars=('gregorian',)):
def fromXmlData(cls, lookup: Callable[[str], str], calendars: Iterable[str]=('gregorian',)
) -> "Locale":
"""Constructor from the contents of XML elements.
First parameter, lookup, is called with the names of XML elements that
@ -546,7 +548,7 @@ class Locale (object):
Optional second parameter, calendars, is a sequence of calendars for
which data is to be retrieved."""
data = {}
data: dict[str, int|str|dict[str, str]] = {}
for k in cls.__asint:
data[k] = int(lookup(k))
@ -566,7 +568,8 @@ class Locale (object):
return cls(data)
def toXml(self, write, calendars=('gregorian',)):
def toXml(self, write: Callable[[str, str], None], calendars: Iterable[str]=('gregorian',)
) -> None:
"""Writes its data as QLocale XML.
First argument, write, is a callable taking the name and
@ -576,7 +579,7 @@ class Locale (object):
Optional second argument is a list of calendar names, in the
form used by CLDR; its default is ('gregorian',).
"""
get = lambda k: getattr(self, k)
get: Callable[[str], str | Iterable[int]] = lambda k: getattr(self, k)
for key in ('language', 'script', 'territory'):
write(key, get(key))
write(f'{key}code', get(f'{key}_code'))
@ -608,7 +611,7 @@ class Locale (object):
write(key, get(key))
@classmethod
def C(cls, en_US):
def C(cls, en_US: "Locale") -> "Locale": # return type should be Self from Python 3.11
"""Returns an object representing the C locale.
Required argument, en_US, is the corresponding object for the