From afa0e9094307988a3c79cc3ded17553508cdae18 Mon Sep 17 00:00:00 2001 From: "Matthias C. M. Troffaes" Date: Fri, 25 Aug 2023 10:10:40 +0200 Subject: [PATCH 1/3] Fix clean_backref for extensions that have backrefs to inline elements. --- numpydoc/numpydoc.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/numpydoc/numpydoc.py b/numpydoc/numpydoc.py index 284c7da1..3af2f11f 100644 --- a/numpydoc/numpydoc.py +++ b/numpydoc/numpydoc.py @@ -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 @@ -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"] = [ From 1627e40b9d5c2590f00c665dad3709e1a5de671c Mon Sep 17 00:00:00 2001 From: "Matthias C. M. Troffaes" Date: Sat, 26 Aug 2023 19:51:07 +0200 Subject: [PATCH 2/3] Add test for inline backrefs. --- numpydoc/tests/test_numpydoc.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/numpydoc/tests/test_numpydoc.py b/numpydoc/tests/test_numpydoc.py index f92bd82f..9f25912a 100644 --- a/numpydoc/tests/test_numpydoc.py +++ b/numpydoc/tests/test_numpydoc.py @@ -3,7 +3,11 @@ 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 @@ -262,6 +266,21 @@ 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 From 6372168587edb52804d6062bc73850d96b885258 Mon Sep 17 00:00:00 2001 From: "Matthias C. M. Troffaes" Date: Sat, 26 Aug 2023 19:57:54 +0200 Subject: [PATCH 3/3] Fix black. --- numpydoc/tests/test_numpydoc.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/numpydoc/tests/test_numpydoc.py b/numpydoc/tests/test_numpydoc.py index 9f25912a..fd8927d5 100644 --- a/numpydoc/tests/test_numpydoc.py +++ b/numpydoc/tests/test_numpydoc.py @@ -6,8 +6,12 @@ from docutils import nodes -from numpydoc.numpydoc import mangle_docstrings, _clean_text_signature, \ - update_config, clean_backrefs +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 @@ -272,7 +276,8 @@ def test_clean_backrefs(): 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"]) + rawsource="", docname="index", backrefs=["id1"], ids=["r123-1"] + ) citation += nodes.label("1") citation += nodes.paragraph(rawsource="", text="Author. Title.") par += inline_ref