Skip to content

Commit 8e98b87

Browse files
committed
use node.findall if available (docutils 18.x)
1 parent 7eec71f commit 8e98b87

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

numpydoc/numpydoc.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@
4343
HASH_LEN = 12
4444

4545

46+
def _traverse_or_findall(node, condition, **kwargs):
47+
"""Triage node.traverse (docutils <0.18) vs node.findall."""
48+
return (
49+
node.findall(condition, **kwargs) if hasattr(node, 'findall') else
50+
node.traverse(condition, **kwargs)
51+
)
52+
53+
4654
def rename_references(app, what, name, obj, options, lines):
4755
# decorate reference numbers so that there are no duplicates
4856
# these are later undecorated in the doctree, in relabel_references
@@ -81,9 +89,8 @@ def is_docstring_section(node):
8189
return False
8290

8391
sibling_sections = itertools.chain(
84-
section_node.traverse(
85-
is_docstring_section, include_self=True, descend=False, siblings=True
86-
)
92+
_traverse_or_findall(section_node, is_docstring_section,
93+
include_self=True, descend=False, siblings=True)
8794
)
8895
for sibling_section in sibling_sections:
8996
if not sibling_section.children:
@@ -101,7 +108,7 @@ def is_docstring_section(node):
101108

102109
def relabel_references(app, doc):
103110
# Change 'hash-ref' to 'ref' in label text
104-
for citation_node in doc.traverse(citation):
111+
for citation_node in _traverse_or_findall(doc, citation):
105112
if not _is_cite_in_numpydoc_docstring(citation_node):
106113
continue
107114
label_node = citation_node[0]
@@ -121,18 +128,19 @@ def matching_pending_xref(node):
121128
and node[0].astext() == f"[{ref_text}]"
122129
)
123130

124-
for xref_node in ref.parent.traverse(matching_pending_xref):
131+
for xref_node in _traverse_or_findall(
132+
ref.parent, matching_pending_xref):
125133
xref_node.replace(xref_node[0], Text(f"[{new_text}]"))
126134
ref.replace(ref_text, new_text.copy())
127135

128136

129137
def clean_backrefs(app, doc, docname):
130138
# only::latex directive has resulted in citation backrefs without reference
131139
known_ref_ids = set()
132-
for ref in doc.traverse(reference, descend=True):
140+
for ref in _traverse_or_findall(doc, reference, descend=True):
133141
for id_ in ref["ids"]:
134142
known_ref_ids.add(id_)
135-
for citation_node in doc.traverse(citation, descend=True):
143+
for citation_node in _traverse_or_findall(doc, citation, descend=True):
136144
# remove backrefs to non-existent refs
137145
citation_node["backrefs"] = [
138146
id_ for id_ in citation_node["backrefs"] if id_ in known_ref_ids

0 commit comments

Comments
 (0)