Merge pull request #9721 from tk0miya/docutils-0.18-traverse

refactor: Node.traverse() will returns generator since 0.18
This commit is contained in:
Takeshi KOMIYA
2021-10-10 16:36:40 +09:00
committed by GitHub
4 changed files with 14 additions and 14 deletions

View File

@@ -298,7 +298,7 @@ class Locale(SphinxTransform):
is_autofootnote_ref = NodeMatcher(nodes.footnote_reference, auto=Any)
old_foot_refs: List[nodes.footnote_reference] = node.traverse(is_autofootnote_ref)
new_foot_refs: List[nodes.footnote_reference] = patch.traverse(is_autofootnote_ref)
if len(old_foot_refs) != len(new_foot_refs):
if len(list(old_foot_refs)) != len(list(new_foot_refs)):
old_foot_ref_rawsources = [ref.rawsource for ref in old_foot_refs]
new_foot_ref_rawsources = [ref.rawsource for ref in new_foot_refs]
logger.warning(__('inconsistent footnote references in translated message.' +
@@ -341,7 +341,7 @@ class Locale(SphinxTransform):
is_refnamed_ref = NodeMatcher(nodes.reference, refname=Any)
old_refs: List[nodes.reference] = node.traverse(is_refnamed_ref)
new_refs: List[nodes.reference] = patch.traverse(is_refnamed_ref)
if len(old_refs) != len(new_refs):
if len(list(old_refs)) != len(list(new_refs)):
old_ref_rawsources = [ref.rawsource for ref in old_refs]
new_ref_rawsources = [ref.rawsource for ref in new_refs]
logger.warning(__('inconsistent references in translated message.' +
@@ -369,7 +369,7 @@ class Locale(SphinxTransform):
old_foot_refs = node.traverse(is_refnamed_footnote_ref)
new_foot_refs = patch.traverse(is_refnamed_footnote_ref)
refname_ids_map: Dict[str, List[str]] = {}
if len(old_foot_refs) != len(new_foot_refs):
if len(list(old_foot_refs)) != len(list(new_foot_refs)):
old_foot_ref_rawsources = [ref.rawsource for ref in old_foot_refs]
new_foot_ref_rawsources = [ref.rawsource for ref in new_foot_refs]
logger.warning(__('inconsistent footnote references in translated message.' +
@@ -388,7 +388,7 @@ class Locale(SphinxTransform):
old_cite_refs: List[nodes.citation_reference] = node.traverse(is_citation_ref)
new_cite_refs: List[nodes.citation_reference] = patch.traverse(is_citation_ref)
refname_ids_map = {}
if len(old_cite_refs) != len(new_cite_refs):
if len(list(old_cite_refs)) != len(list(new_cite_refs)):
old_cite_ref_rawsources = [ref.rawsource for ref in old_cite_refs]
new_cite_ref_rawsources = [ref.rawsource for ref in new_cite_refs]
logger.warning(__('inconsistent citation references in translated message.' +
@@ -408,7 +408,7 @@ class Locale(SphinxTransform):
old_xrefs = node.traverse(addnodes.pending_xref)
new_xrefs = patch.traverse(addnodes.pending_xref)
xref_reftarget_map = {}
if len(old_xrefs) != len(new_xrefs):
if len(list(old_xrefs)) != len(list(new_xrefs)):
old_xref_rawsources = [xref.rawsource for xref in old_xrefs]
new_xref_rawsources = [xref.rawsource for xref in new_xrefs]
logger.warning(__('inconsistent term references in translated message.' +

View File

@@ -1011,7 +1011,7 @@ class LaTeXTranslator(SphinxTranslator):
context = (r'\par' + CR + r'\vskip-\baselineskip'
r'\vbox{\hbox{\strut}}\end{varwidth}%' + CR + context)
self.needs_linetrimming = 1
if len(node.traverse(nodes.paragraph)) >= 2:
if len(list(node.traverse(nodes.paragraph))) >= 2:
self.table.has_oldproblematic = True
if isinstance(node.parent.parent, nodes.thead) or (cell.col in self.table.stubs):
if len(node) == 1 and isinstance(node[0], nodes.paragraph) and node.astext() == '':

View File

@@ -850,7 +850,7 @@ class TextTranslator(SphinxTranslator):
self.end_state(first='%s. ' % self.list_counter[-1])
def visit_definition_list_item(self, node: Element) -> None:
self._classifier_count_in_li = len(node.traverse(nodes.classifier))
self._classifier_count_in_li = len(list(node.traverse(nodes.classifier)))
def depart_definition_list_item(self, node: Element) -> None:
pass

View File

@@ -60,31 +60,31 @@ def test_NodeMatcher():
# search by node class
matcher = NodeMatcher(nodes.paragraph)
assert len(doctree.traverse(matcher)) == 3
assert len(list(doctree.traverse(matcher))) == 3
# search by multiple node classes
matcher = NodeMatcher(nodes.paragraph, nodes.literal_block)
assert len(doctree.traverse(matcher)) == 4
assert len(list(doctree.traverse(matcher))) == 4
# search by node attribute
matcher = NodeMatcher(block=1)
assert len(doctree.traverse(matcher)) == 1
assert len(list(doctree.traverse(matcher))) == 1
# search by node attribute (Any)
matcher = NodeMatcher(block=Any)
assert len(doctree.traverse(matcher)) == 3
assert len(list(doctree.traverse(matcher))) == 3
# search by both class and attribute
matcher = NodeMatcher(nodes.paragraph, block=Any)
assert len(doctree.traverse(matcher)) == 2
assert len(list(doctree.traverse(matcher))) == 2
# mismatched
matcher = NodeMatcher(nodes.title)
assert len(doctree.traverse(matcher)) == 0
assert len(list(doctree.traverse(matcher))) == 0
# search with Any does not match to Text node
matcher = NodeMatcher(blah=Any)
assert len(doctree.traverse(matcher)) == 0
assert len(list(doctree.traverse(matcher))) == 0
@pytest.mark.parametrize(