Skip to content

Commit 307992e

Browse files
authored
BUG: Fix clean_backref for extensions that have backrefs to inline elements. (#499)
* Fix clean_backref for extensions that have backrefs to inline elements. * Add test for inline backrefs.
1 parent 6e748ef commit 307992e

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

numpydoc/numpydoc.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import hashlib
2525
import itertools
2626

27-
from docutils.nodes import citation, Text, section, comment, reference
27+
from docutils.nodes import citation, Text, section, comment, reference, inline
2828
import sphinx
2929
from sphinx.addnodes import pending_xref, desc_content
3030
from sphinx.util import logging
@@ -149,6 +149,10 @@ def clean_backrefs(app, doc, docname):
149149
for ref in _traverse_or_findall(doc, reference, descend=True):
150150
for id_ in ref["ids"]:
151151
known_ref_ids.add(id_)
152+
# some extensions produce backrefs to inline elements
153+
for ref in _traverse_or_findall(doc, inline, descend=True):
154+
for id_ in ref["ids"]:
155+
known_ref_ids.add(id_)
152156
for citation_node in _traverse_or_findall(doc, citation, descend=True):
153157
# remove backrefs to non-existent refs
154158
citation_node["backrefs"] = [

numpydoc/tests/test_numpydoc.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33
from io import StringIO
44
from pathlib import PosixPath
55
from copy import deepcopy
6-
from numpydoc.numpydoc import mangle_docstrings, _clean_text_signature, update_config
6+
7+
from docutils import nodes
8+
9+
from numpydoc.numpydoc import (
10+
mangle_docstrings,
11+
_clean_text_signature,
12+
update_config,
13+
clean_backrefs,
14+
)
715
from numpydoc.xref import DEFAULT_LINKS
816
from sphinx.ext.autodoc import ALL
917
from sphinx.util import logging
@@ -262,6 +270,22 @@ def test_update_config_exclude_str():
262270
update_config(app)
263271

264272

273+
def test_clean_backrefs():
274+
"""Check ids are not cleaned from inline backrefs."""
275+
par = nodes.paragraph(rawsource="", text="")
276+
inline_ref = nodes.inline(rawsource="", text="", ids=["id1"])
277+
inline_ref += nodes.reference(rawsource="", text="[1]", refid="r123-1")
278+
citation = nodes.citation(
279+
rawsource="", docname="index", backrefs=["id1"], ids=["r123-1"]
280+
)
281+
citation += nodes.label("1")
282+
citation += nodes.paragraph(rawsource="", text="Author. Title.")
283+
par += inline_ref
284+
par += citation
285+
clean_backrefs(app=MockApp(), doc=par, docname="index")
286+
assert "id1" in citation["backrefs"]
287+
288+
265289
if __name__ == "__main__":
266290
import pytest
267291

0 commit comments

Comments
 (0)