Skip to content

Commit 13eefcd

Browse files
committed
Refer to source code on Github in API docs.
1 parent 67e328b commit 13eefcd

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

docs/source/changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ releases are available on [PyPI](https://pypi.org/project/pytask) and
1212
- {pull}`454` removes more `.svg`s and replaces them with animations.
1313
- {pull}`455` adds more explanation when {meth}`~pytask.PNode.load` fails during the
1414
execution.
15+
- {pull}`456` refers to the source code on Github when clicking on a source link.
1516

1617
## 0.4.1 - 2023-10-11
1718

docs/source/conf.py

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@
66
"""
77
from __future__ import annotations
88

9+
import inspect
10+
import os
11+
import sys
12+
import warnings
913
from importlib.metadata import version
14+
from pathlib import Path
1015
from typing import TYPE_CHECKING
1116

17+
import pytask
18+
1219
if TYPE_CHECKING:
1320
import sphinx
1421

@@ -36,9 +43,9 @@
3643
"sphinx.ext.autodoc",
3744
"sphinx.ext.extlinks",
3845
"sphinx.ext.intersphinx",
46+
"sphinx.ext.linkcode",
3947
"sphinx.ext.napoleon",
4048
"sphinxext.opengraph",
41-
"sphinx.ext.viewcode",
4249
"sphinx_copybutton",
4350
"sphinx_click",
4451
"sphinx_toolbox.more_autodoc.autoprotocol",
@@ -90,6 +97,63 @@
9097
ogp_social_cards = {"image": "_static/images/pytask_w_text.png"}
9198

9299

100+
# Linkcode, based on numpy doc/source/conf.py
101+
def linkcode_resolve(domain: str, info: dict[str, str]) -> str: # noqa: C901, PLR0912
102+
"""Determine the URL corresponding to Python object."""
103+
if domain != "py":
104+
return None
105+
106+
modname = info["module"]
107+
fullname = info["fullname"]
108+
109+
submod = sys.modules.get(modname)
110+
if submod is None:
111+
return None
112+
113+
obj = submod
114+
for part in fullname.split("."):
115+
try:
116+
with warnings.catch_warnings():
117+
# Accessing deprecated objects will generate noisy warnings
118+
warnings.simplefilter("ignore", FutureWarning)
119+
obj = getattr(obj, part)
120+
except AttributeError: # noqa: PERF203
121+
return None
122+
123+
try:
124+
fn = inspect.getsourcefile(inspect.unwrap(obj))
125+
except TypeError:
126+
try: # property
127+
fn = inspect.getsourcefile(inspect.unwrap(obj.fget))
128+
except (AttributeError, TypeError):
129+
fn = None
130+
if not fn:
131+
return None
132+
133+
try:
134+
source, lineno = inspect.getsourcelines(obj)
135+
except TypeError:
136+
try: # property
137+
source, lineno = inspect.getsourcelines(obj.fget)
138+
except (AttributeError, TypeError):
139+
lineno = None
140+
except OSError:
141+
lineno = None
142+
143+
linespec = f"#L{lineno}-L{lineno + len(source) - 1}" if lineno else ""
144+
145+
fn = os.path.relpath(fn, start=Path(pytask.__file__).parent)
146+
147+
if "+" in pytask.__version__:
148+
return (
149+
f"https://github.com/pytask-dev/pytask/blob/main/src/pytask/{fn}{linespec}"
150+
)
151+
return (
152+
f"https://github.com/pytask-dev/pytask/blob/"
153+
f"v{pytask.__version__}/src/pytask/{fn}{linespec}"
154+
)
155+
156+
93157
# -- Options for HTML output -----------------------------------------------------------
94158

95159
# The theme to use for HTML and HTML Help pages. See the documentation for a list of

0 commit comments

Comments
 (0)