Add type annotations to XmlScanner and Supplement in ldml.py

Task-number: QTBUG-129566
Pick-to: 6.8
Change-Id: I0300e97222c9d4b3e521a147e58c948c0015ad59
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Mate Barany 2024-10-04 15:55:52 +02:00
parent d52506e262
commit 2b167291be

View File

@ -165,15 +165,16 @@ def _iterateEach(iters: Iterator[Iterator[Any]]) -> Iterator[Any]:
class XmlScanner (object): class XmlScanner (object):
"""Wrap an XML file to enable XPath access to its nodes. """Wrap an XML file to enable XPath access to its nodes.
""" """
def __init__(self, node): def __init__(self, node: Node) -> None:
self.root = node self.root = node
def findNodes(self, xpath): def findNodes(self, xpath: str) -> tuple[Node, ...]:
"""Return all nodes under self.root matching this xpath. """Return all nodes under self.root matching this xpath.
Ignores any excess attributes.""" Ignores any excess attributes."""
elts = (self.root,) elts: tuple[Node, ...] = (self.root,)
for selector in xpath.split('/'): for selector in xpath.split('/'):
# tag is a str and attrs is a dict[str, str]
tag, attrs = _parseXPath(selector) tag, attrs = _parseXPath(selector)
elts = tuple(_iterateEach(e.findAllChildren(tag, attrs) for e in elts)) elts = tuple(_iterateEach(e.findAllChildren(tag, attrs) for e in elts))
if not elts: if not elts:
@ -181,7 +182,8 @@ class XmlScanner (object):
return elts return elts
class Supplement (XmlScanner): class Supplement (XmlScanner):
def find(self, xpath, exclude=()): def find(self, xpath: str,
exclude: tuple[str, ...] = ()) -> Iterator[tuple[str, dict[str, str]]]:
"""Finds nodes by matching a specified xpath. """Finds nodes by matching a specified xpath.
If exclude is passed, it should be a sequence of attribute names (its If exclude is passed, it should be a sequence of attribute names (its
@ -193,7 +195,7 @@ class Supplement (XmlScanner):
nodeName and attrs is a dict mapping the node's attribute's names to nodeName and attrs is a dict mapping the node's attribute's names to
their values. For attribute values that are not simple strings, the their values. For attribute values that are not simple strings, the
nodeValue of the attribute node is used.""" nodeValue of the attribute node is used."""
elts = self.findNodes(xpath) elts: tuple[Node, ...] = self.findNodes(xpath)
for elt in _iterateEach(e.dom.childNodes or (e.dom,) for elt in _iterateEach(e.dom.childNodes or (e.dom,)
for e in elts for e in elts
if not any(a in e.dom.attributes if not any(a in e.dom.attributes