Skip to content

Commit

Permalink
BUG: Fix clean_backref for extensions that have backrefs to inline el…
Browse files Browse the repository at this point in the history
…ements. (#499)

* Fix clean_backref for extensions that have backrefs to inline elements.

* Add test for inline backrefs.
  • Loading branch information
mcmtroffaes committed Aug 26, 2023
1 parent 6e748ef commit 307992e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
6 changes: 5 additions & 1 deletion numpydoc/numpydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import hashlib
import itertools

from docutils.nodes import citation, Text, section, comment, reference
from docutils.nodes import citation, Text, section, comment, reference, inline
import sphinx
from sphinx.addnodes import pending_xref, desc_content
from sphinx.util import logging
Expand Down Expand Up @@ -149,6 +149,10 @@ def clean_backrefs(app, doc, docname):
for ref in _traverse_or_findall(doc, reference, descend=True):
for id_ in ref["ids"]:
known_ref_ids.add(id_)
# some extensions produce backrefs to inline elements
for ref in _traverse_or_findall(doc, inline, descend=True):
for id_ in ref["ids"]:
known_ref_ids.add(id_)
for citation_node in _traverse_or_findall(doc, citation, descend=True):
# remove backrefs to non-existent refs
citation_node["backrefs"] = [
Expand Down
26 changes: 25 additions & 1 deletion numpydoc/tests/test_numpydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@
from io import StringIO
from pathlib import PosixPath
from copy import deepcopy
from numpydoc.numpydoc import mangle_docstrings, _clean_text_signature, update_config

from docutils import nodes

from numpydoc.numpydoc import (
mangle_docstrings,
_clean_text_signature,
update_config,
clean_backrefs,
)
from numpydoc.xref import DEFAULT_LINKS
from sphinx.ext.autodoc import ALL
from sphinx.util import logging
Expand Down Expand Up @@ -262,6 +270,22 @@ def test_update_config_exclude_str():
update_config(app)


def test_clean_backrefs():
"""Check ids are not cleaned from inline backrefs."""
par = nodes.paragraph(rawsource="", text="")
inline_ref = nodes.inline(rawsource="", text="", ids=["id1"])
inline_ref += nodes.reference(rawsource="", text="[1]", refid="r123-1")
citation = nodes.citation(
rawsource="", docname="index", backrefs=["id1"], ids=["r123-1"]
)
citation += nodes.label("1")
citation += nodes.paragraph(rawsource="", text="Author. Title.")
par += inline_ref
par += citation
clean_backrefs(app=MockApp(), doc=par, docname="index")
assert "id1" in citation["backrefs"]


if __name__ == "__main__":
import pytest

Expand Down

0 comments on commit 307992e

Please sign in to comment.