Skip to content

Commit ad6251a

Browse files
committed
docs(conf): Add missing-reference handler to resolve py:data as :class: links
why: sphinx-autodoc-pytest-fixtures wraps every fixture return-type identifier in a :class: cross-reference so that Sphinx and intersphinx can resolve it. Sphinx 8.x's Python domain only searches py:class and py:exception entries for :class: references; TypeAliases land in the inventory as py:data instead. The result is a broken (unlinked) type name in the fixture summary table for any TypeAlias return type. The same handler was added to unihan-etl's conf.py for the same reason; this brings libvcs in line with that approach. what: - Import typing as t at the top of conf.py - Guard docutils / sphinx type imports under TYPE_CHECKING so mypy gets accurate types without a runtime dependency on types-docutils being present in every environment - Add _on_missing_class_reference(): when a :class: reference cannot be resolved, fall back to searching all Python-domain objects so that py:data entries (TypeAliases) are found and linked - Add setup(app) to wire the handler into Sphinx's missing-reference event
1 parent db8e3d5 commit ad6251a

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

docs/conf.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
import pathlib
66
import sys
7+
import typing as t
8+
9+
if t.TYPE_CHECKING:
10+
from docutils import nodes
11+
from sphinx import addnodes
12+
from sphinx.application import Sphinx
13+
from sphinx.environment import BuildEnvironment
714

815
from gp_sphinx.config import make_linkcode_resolve, merge_sphinx_config
916

@@ -54,3 +61,33 @@
5461
rediraffe_redirects="redirects.txt",
5562
)
5663
globals().update(conf)
64+
65+
66+
def _on_missing_class_reference(
67+
app: Sphinx,
68+
env: BuildEnvironment,
69+
node: addnodes.pending_xref,
70+
contnode: nodes.TextElement,
71+
) -> nodes.reference | None:
72+
if node.get("refdomain") != "py" or node.get("reftype") != "class":
73+
return None
74+
from sphinx.util.nodes import make_refnode
75+
76+
py_domain = env.get_domain("py")
77+
target = node.get("reftarget", "")
78+
matches = py_domain.find_obj(env, "", "", target, None, 1)
79+
if not matches:
80+
return None
81+
_name, obj_entry = matches[0]
82+
return make_refnode(
83+
app.builder,
84+
node.get("refdoc", ""),
85+
obj_entry.docname,
86+
obj_entry.node_id,
87+
contnode,
88+
)
89+
90+
91+
def setup(app: Sphinx) -> None:
92+
"""Connect missing-reference handler to resolve py:data as :class: links."""
93+
app.connect("missing-reference", _on_missing_class_reference)

0 commit comments

Comments
 (0)