Add type annotations to Spacer

Task-number: QTBUG-129564
Pick-to: 6.8
Change-Id: I942d135da630f7ba6641170a1a597b0578aca878
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
This commit is contained in:
Mate Barany 2024-10-25 14:38:31 +02:00
parent 05b5de9e37
commit 519d3d36c3

View File

@ -571,7 +571,7 @@ class QLocaleXmlReader (object):
class Spacer (object): class Spacer (object):
def __init__(self, indent = None, initial = ''): def __init__(self, indent:str|int|None = None, initial: str = '') -> None:
"""Prepare to manage indentation and line breaks. """Prepare to manage indentation and line breaks.
Arguments are both optional. Arguments are both optional.
@ -590,17 +590,17 @@ class Spacer (object):
an end-tag. The text is not parsed any more carefully than an end-tag. The text is not parsed any more carefully than
just described.""" just described."""
if indent is None: if indent is None:
self.__call = lambda x: x self.__call: Callable[[str], str] = lambda x: x
else: else:
self.__each = ' ' * indent if isinstance(indent, int) else indent self.__each: str = ' ' * indent if isinstance(indent, int) else indent
self.current = initial self.current = initial
self.__call = self.__wrap self.__call = self.__wrap
def __wrap(self, line): def __wrap(self, line: str) -> str:
if not line: if not line:
return '\n' return '\n'
indent = self.current indent: str = self.current
if line.startswith('</'): if line.startswith('</'):
indent = self.current = indent[:-len(self.__each)] indent = self.current = indent[:-len(self.__each)]
elif line.startswith('<') and line[1:2] not in '!?': elif line.startswith('<') and line[1:2] not in '!?':
@ -611,7 +611,7 @@ class Spacer (object):
self.current += self.__each self.current += self.__each
return indent + line + '\n' return indent + line + '\n'
def __call__(self, line): def __call__(self, line: str) -> str:
return self.__call(line) return self.__call(line)
class QLocaleXmlWriter (object): class QLocaleXmlWriter (object):