Skip to content

Commit 76c033b

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

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

numpydoc/numpydoc.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@
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)
50+
if hasattr(node, "findall")
51+
else node.traverse(condition, **kwargs)
52+
)
53+
54+
4655
def rename_references(app, what, name, obj, options, lines):
4756
# decorate reference numbers so that there are no duplicates
4857
# these are later undecorated in the doctree, in relabel_references
@@ -81,8 +90,12 @@ def is_docstring_section(node):
8190
return False
8291

8392
sibling_sections = itertools.chain(
84-
section_node.traverse(
85-
is_docstring_section, include_self=True, descend=False, siblings=True
93+
_traverse_or_findall(
94+
section_node,
95+
is_docstring_section,
96+
include_self=True,
97+
descend=False,
98+
siblings=True,
8699
)
87100
)
88101
for sibling_section in sibling_sections:
@@ -101,7 +114,7 @@ def is_docstring_section(node):
101114

102115
def relabel_references(app, doc):
103116
# Change 'hash-ref' to 'ref' in label text
104-
for citation_node in doc.traverse(citation):
117+
for citation_node in _traverse_or_findall(doc, citation):
105118
if not _is_cite_in_numpydoc_docstring(citation_node):
106119
continue
107120
label_node = citation_node[0]
@@ -121,18 +134,18 @@ def matching_pending_xref(node):
121134
and node[0].astext() == f"[{ref_text}]"
122135
)
123136

124-
for xref_node in ref.parent.traverse(matching_pending_xref):
137+
for xref_node in _traverse_or_findall(ref.parent, matching_pending_xref):
125138
xref_node.replace(xref_node[0], Text(f"[{new_text}]"))
126139
ref.replace(ref_text, new_text.copy())
127140

128141

129142
def clean_backrefs(app, doc, docname):
130143
# only::latex directive has resulted in citation backrefs without reference
131144
known_ref_ids = set()
132-
for ref in doc.traverse(reference, descend=True):
145+
for ref in _traverse_or_findall(doc, reference, descend=True):
133146
for id_ in ref["ids"]:
134147
known_ref_ids.add(id_)
135-
for citation_node in doc.traverse(citation, descend=True):
148+
for citation_node in _traverse_or_findall(doc, citation, descend=True):
136149
# remove backrefs to non-existent refs
137150
citation_node["backrefs"] = [
138151
id_ for id_ in citation_node["backrefs"] if id_ in known_ref_ids

0 commit comments

Comments
 (0)