Skip to content

Commit

Permalink
Merge pull request #9721 from tk0miya/docutils-0.18-traverse
Browse files Browse the repository at this point in the history
refactor: Node.traverse() will returns generator since 0.18
  • Loading branch information
tk0miya committed Oct 10, 2021
2 parents 3aabcd2 + 179e0bf commit 3b8456f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
10 changes: 5 additions & 5 deletions sphinx/transforms/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def list_replace_or_append(lst: List[N], old: N, new: N) -> None:
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.' +
Expand Down Expand Up @@ -341,7 +341,7 @@ def list_replace_or_append(lst: List[N], old: N, new: N) -> None:
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.' +
Expand Down Expand Up @@ -369,7 +369,7 @@ def list_replace_or_append(lst: List[N], old: N, new: N) -> None:
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.' +
Expand All @@ -388,7 +388,7 @@ def list_replace_or_append(lst: List[N], old: N, new: N) -> None:
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.' +
Expand All @@ -408,7 +408,7 @@ def list_replace_or_append(lst: List[N], old: N, new: N) -> None:
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.' +
Expand Down
2 changes: 1 addition & 1 deletion sphinx/writers/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ def visit_entry(self, node: Element) -> None:
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() == '':
Expand Down
2 changes: 1 addition & 1 deletion sphinx/writers/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ def depart_list_item(self, node: Element) -> None:
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
Expand Down
14 changes: 7 additions & 7 deletions tests/test_util_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 3b8456f

Please sign in to comment.