Skip to content

Commit 80861bb

Browse files
committed
docs(conf): Rewrite Pydantic markdown cross-refs in autodoc docstrings
why: Pydantic's BaseModel uses Markdown-style [text][ref] cross-refs in docstrings, but autodoc processes them as RST, yielding citation-ref rendering instead of hyperlinks. what: - autodoc-process-docstring hook rewrites [`Name`][pkg.Name] to :any: roles - pyproject.toml: add fastmcp_autodoc to mypy ignore_missing_imports
1 parent e42b271 commit 80861bb

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

docs/conf.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
import pathlib
6+
import re
67
import sys
78
import typing as t
89

@@ -103,10 +104,30 @@
103104

104105
_gp_setup = conf.pop("setup")
105106

107+
# Matches Pydantic-style markdown cross-refs in RST docstrings:
108+
# [DisplayText][qualified.Name] → :any:`DisplayText <qualified.Name>`
109+
# [`DisplayText`][qualified.Name] → :any:`DisplayText <qualified.Name>`
110+
# Display text may be wrapped in backticks — strip them before forming the role.
111+
_MD_XREF = re.compile(r"\[`?([^`\]]+)`?\]\[([a-zA-Z_][a-zA-Z0-9_.]*)\]")
112+
113+
114+
def _convert_md_xrefs(
115+
app: Sphinx,
116+
what: str,
117+
name: str,
118+
obj: object,
119+
options: object,
120+
lines: list[str],
121+
) -> None:
122+
"""Rewrite Pydantic markdown cross-refs to RST :any: roles."""
123+
for i, line in enumerate(lines):
124+
lines[i] = _MD_XREF.sub(r":any:`\1 <\2>`", line)
125+
106126

107127
def setup(app: Sphinx) -> None:
108128
"""Configure Sphinx app hooks and register project-specific JS."""
109129
_gp_setup(app)
130+
app.connect("autodoc-process-docstring", _convert_md_xrefs)
110131
app.add_js_file("js/prompt-copy.js", loading_method="defer")
111132

112133

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ files = [
123123
]
124124

125125
[[tool.mypy.overrides]]
126-
module = ["sphinx_autodoc_fastmcp", "sphinx_autodoc_fastmcp.*", "docutils", "docutils.*"]
126+
module = ["fastmcp_autodoc", "sphinx_autodoc_fastmcp", "sphinx_autodoc_fastmcp.*", "docutils", "docutils.*"]
127127
ignore_missing_imports = true
128128

129129
[[tool.mypy.overrides]]

0 commit comments

Comments
 (0)