Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clean_backref for extensions that have backrefs to inline elements. #499

Merged
merged 3 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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