Skip to content

Commit 0a67654

Browse files
timsaucerclaude
andcommitted
docs: use sphinx cross-ref roles in docstrings for IDE rendering
JetBrains and other IDEs do not understand mkdocstrings autoref syntax (`[name][path]`) and display it as literal text in docstring hovers. Switch docstring cross-references to sphinx-style roles (:func:, :class:, :meth:, :attr:, :mod:, :exc:) which IDEs render natively as clickable links. A new griffe extension (`docs/griffe_extensions.py`) rewrites the sphinx roles back into mkdocstrings autorefs before mkdocstrings parses each docstring, so the docs site continues to produce working cross-reference links. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 03ab28d commit 0a67654

15 files changed

Lines changed: 359 additions & 298 deletions

docs/griffe_extensions.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
"""Griffe extensions for datafusion-python docs.
19+
20+
`SphinxRefsToAutorefs` rewrites sphinx-style cross-reference roles
21+
(``:func:`~path`, :class:`~path``, etc.) inside docstrings into
22+
mkdocstrings autoref syntax (``[`tail`][path]``) so that the same
23+
docstring renders as a clickable cross-reference both in JetBrains-style
24+
IDEs (which understand sphinx roles) and on the published docs site
25+
(which understands mkdocstrings autorefs).
26+
"""
27+
28+
from __future__ import annotations
29+
30+
import re
31+
from typing import Any
32+
33+
from griffe import Extension, Object
34+
35+
_ROLE_RE = re.compile(
36+
r":(?:py:)?(?P<role>func|class|meth|attr|mod|obj|exc|const|data)"
37+
r":`(?P<tilde>~?)(?P<target>[\w.]+)`"
38+
)
39+
40+
41+
def _rewrite(text: str) -> str:
42+
def repl(match: re.Match[str]) -> str:
43+
target = match.group("target")
44+
tail = target.rsplit(".", 1)[-1]
45+
return f"[`{tail}`][{target}]"
46+
47+
return _ROLE_RE.sub(repl, text)
48+
49+
50+
class SphinxRefsToAutorefs(Extension):
51+
"""Convert sphinx-style cross-references into mkdocstrings autorefs."""
52+
53+
def on_object(self, *, obj: Object, **_: Any) -> None:
54+
docstring = obj.docstring
55+
if docstring is None:
56+
return
57+
new = _rewrite(docstring.value)
58+
if new != docstring.value:
59+
docstring.value = new

mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ plugins:
6767
- https://arrow.apache.org/docs/objects.inv
6868
- https://docs.pola.rs/api/python/stable/objects.inv
6969
options:
70+
extensions:
71+
- docs/griffe_extensions.py:SphinxRefsToAutorefs
7072
docstring_style: google
7173
docstring_options:
7274
warn_unknown_params: false

python/datafusion/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
- **SessionContext** -- entry point for loading data, running SQL, and creating
2828
DataFrames.
2929
- **DataFrame** -- lazy query builder. Every method returns a new DataFrame;
30-
call [`collect`][datafusion.dataframe.DataFrame.collect] or a ``to_*``
30+
call :meth:`~datafusion.dataframe.DataFrame.collect` or a ``to_*``
3131
method to execute.
3232
- **Expr** -- expression tree node for column references, literals, and function
33-
calls. Build with [`col`][datafusion.col.col] and [`lit`][datafusion.lit].
33+
calls. Build with :func:`~datafusion.col.col` and :func:`~datafusion.lit`.
3434
- **functions** -- 290+ built-in scalar, aggregate, and window functions.
3535
3636
Examples:

python/datafusion/catalog.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ def __repr__(self) -> str:
220220
@staticmethod
221221
@deprecated("Use Table() constructor instead.")
222222
def from_dataset(dataset: pa.dataset.Dataset) -> Table:
223-
"""Turn a `dataset` ``Dataset`` into a [`Table`][datafusion.catalog.Table]."""
223+
"""Turn a `dataset` ``Dataset`` into a :class:`~datafusion.catalog.Table`."""
224224
return Table(dataset)
225225

226226
@property

0 commit comments

Comments
 (0)