Skip to content

Commit af936a1

Browse files
committed
Use node.findall (docutils 18.x)
1 parent c645a9d commit af936a1

File tree

2 files changed

+9
-33
lines changed

2 files changed

+9
-33
lines changed

numpydoc/numpydoc.py

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,6 @@
3939
HASH_LEN = 12
4040

4141

42-
def _traverse_or_findall(node, condition, **kwargs):
43-
"""Triage node.traverse (docutils <0.18.1) vs node.findall.
44-
45-
TODO: This check can be removed when the minimum supported docutils version
46-
for numpydoc is docutils>=0.18.1
47-
"""
48-
return (
49-
node.findall(condition, **kwargs)
50-
if hasattr(node, "findall")
51-
else node.traverse(condition, **kwargs)
52-
)
53-
54-
5542
def rename_references(app, what, name, obj, options, lines):
5643
# decorate reference numbers so that there are no duplicates
5744
# these are later undecorated in the doctree, in relabel_references
@@ -92,12 +79,8 @@ def is_docstring_section(node):
9279
return False
9380

9481
sibling_sections = itertools.chain(
95-
_traverse_or_findall(
96-
section_node,
97-
is_docstring_section,
98-
include_self=True,
99-
descend=False,
100-
siblings=True,
82+
section_node.findall(
83+
is_docstring_section, include_self=True, descend=False, siblings=True
10184
)
10285
)
10386
for sibling_section in sibling_sections:
@@ -116,7 +99,7 @@ def is_docstring_section(node):
11699

117100
def relabel_references(app, doc):
118101
# Change 'hash-ref' to 'ref' in label text
119-
for citation_node in _traverse_or_findall(doc, citation):
102+
for citation_node in doc.findall(citation):
120103
if not _is_cite_in_numpydoc_docstring(citation_node):
121104
continue
122105
label_node = citation_node[0]
@@ -136,22 +119,22 @@ def matching_pending_xref(node):
136119
and node[0].astext() == f"[{ref_text}]"
137120
)
138121

139-
for xref_node in _traverse_or_findall(ref.parent, matching_pending_xref):
122+
for xref_node in ref.parent.findall(matching_pending_xref):
140123
xref_node.replace(xref_node[0], Text(f"[{new_text}]"))
141124
ref.replace(ref_text, new_text.copy())
142125

143126

144127
def clean_backrefs(app, doc, docname):
145128
# only::latex directive has resulted in citation backrefs without reference
146129
known_ref_ids = set()
147-
for ref in _traverse_or_findall(doc, reference, descend=True):
130+
for ref in doc.findall(reference, descend=True):
148131
for id_ in ref["ids"]:
149132
known_ref_ids.add(id_)
150133
# some extensions produce backrefs to inline elements
151-
for ref in _traverse_or_findall(doc, inline, descend=True):
134+
for ref in doc.findall(inline, descend=True):
152135
for id_ in ref["ids"]:
153136
known_ref_ids.add(id_)
154-
for citation_node in _traverse_or_findall(doc, citation, descend=True):
137+
for citation_node in doc.findall(citation, descend=True):
155138
# remove backrefs to non-existent refs
156139
citation_node["backrefs"] = [
157140
id_ for id_ in citation_node["backrefs"] if id_ in known_ref_ids

numpydoc/tests/test_full.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import shutil
44

55
import pytest
6-
from docutils import __version__ as docutils_version
7-
from packaging import version
86
from sphinx.application import Sphinx
97
from sphinx.util.docutils import docutils_namespace
108

@@ -90,14 +88,9 @@ def test_reference(sphinx_app, html_file, expected_length):
9088
with open(op.join(out_dir, *html_file)) as fid:
9189
html = fid.read()
9290

93-
# TODO: This check can be removed when the minimum supported docutils version
94-
# for numpydoc is docutils>=0.18
95-
pattern = (
96-
'role="doc-backlink"'
97-
if version.parse(docutils_version) >= version.parse("0.18")
98-
else 'class="fn-backref"'
91+
reference_list = re.findall(
92+
r'<a role="doc-backlink" href="\#id\d+">(.*)<\/a>', html
9993
)
100-
reference_list = re.findall(rf'<a {pattern} href="\#id\d+">(.*)<\/a>', html)
10194

10295
assert len(reference_list) == expected_length
10396
for ref in reference_list:

0 commit comments

Comments
 (0)