Skip to content

Commit 97788f0

Browse files
Use sphinx.ext.linkcode for more precise source code links (#11851)
* switched from sphinx.ext.viewcode to sphinx.ext.linkcode * removed extra line * Add section header for source code links Co-authored-by: Eric Arellano <[email protected]> * removed docstring Co-authored-by: Eric Arellano <[email protected]> * update return string Co-authored-by: Eric Arellano <[email protected]> * added back blank line * Added a method to determine the GitHub branch Co-authored-by: Eric Arellano <[email protected]> * add blank line Co-authored-by: Eric Arellano <[email protected]> * remove print statement Co-authored-by: Eric Arellano <[email protected]> * Try to fix error for contextlib file We got this stacktrace: Traceback (most recent call last): File "/home/vsts/work/1/s/.tox/docs/lib/python3.8/site-packages/sphinx/events.py", line 96, in emit results.append(listener.handler(self.app, *args)) File "/home/vsts/work/1/s/.tox/docs/lib/python3.8/site-packages/sphinx/ext/linkcode.py", line 55, in doctree_read uri = resolve_target(domain, info) File "/home/vsts/work/1/s/docs/conf.py", line 216, in linkcode_resolve file_name = PurePath(full_file_name).relative_to(repo_root) File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/pathlib.py", line 908, in relative_to raise ValueError("{!r} does not start with {!r}" ValueError: '/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/contextlib.py' does not start with '/home/vsts/work/1/s' We shouldn't even attempt to generate a link for the file contextlib.py * Try to fix error for Jenkins run #20240221.52 New build failed with this stacktrace: Traceback (most recent call last): File "/home/vsts/work/1/s/.tox/docs/lib/python3.8/site-packages/sphinx/events.py", line 96, in emit results.append(listener.handler(self.app, *args)) File "/home/vsts/work/1/s/.tox/docs/lib/python3.8/site-packages/sphinx/ext/linkcode.py", line 55, in doctree_read uri = resolve_target(domain, info) File "/home/vsts/work/1/s/docs/conf.py", line 215, in linkcode_resolve full_file_name = inspect.getsourcefile(obj) File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/inspect.py", line 696, in getsourcefile filename = getfile(object) File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/inspect.py", line 665, in getfile raise TypeError('{!r} is a built-in class'.format(object)) TypeError: <class 'builtins.CustomClassical'> is a built-in class So I added a condition that the obj should not be a built-in * Check that qiskit in module name sooner * moved valid code object verification earlier * added try except statement to getattr call * added extra try/except block * Also support Azure Pipelines * removed unused import * Revert Azure support to keep things simple * added extra "/" to final URL * Move GitHub branch logic to GitHub Action * switched to importlib and removed redundant block of code * Apply suggestions from code review Co-authored-by: Eric Arellano <[email protected]> * added back spaces * Clarify docs_deploy GitHub logic 1. Remove misleading PR conditional. This worfklow doesn't even run in PRs. It was bad copy-pasta from the runtime repo 2. Clarify why we point tag builds to their stable branch name * Use pathlib for relativizing file name * Fix relative_to() path * Remove tox prefix --------- Co-authored-by: Eric Arellano <[email protected]>
1 parent b797a29 commit 97788f0

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

.github/workflows/docs_deploy.yml

+13-1
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,21 @@ jobs:
2929
- name: Install dependencies
3030
run: tools/install_ubuntu_docs_dependencies.sh
3131

32+
- name: Determine GitHub branch name
33+
run: |
34+
# Tags like 1.0.0 and 1.0.0rc1 should point to their stable branch. We do this
35+
# to reduce the diff in the qiskit/documentation repository between generating
36+
# the API docs from a tag release versus a workflow_dispatch.
37+
if [[ $GITHUB_REF_NAME =~ ^([0-9]+\.[0-9]+) ]]; then
38+
BRANCH_NAME="stable/${BASH_REMATCH[1]}"
39+
else
40+
BRANCH_NAME="$GITHUB_REF_NAME"
41+
fi
42+
echo "Using branch '${BRANCH_NAME}' for GitHub source code links"
43+
echo "QISKIT_DOCS_GITHUB_BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
44+
3245
- name: Build documentation
3346
run: tox run -e docs
34-
3547
- name: Store built documentation artifact
3648
uses: actions/upload-artifact@v4
3749
with:

docs/conf.py

+58-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818

1919
import datetime
2020
import doctest
21+
import importlib
22+
import inspect
23+
import os
24+
import re
25+
from pathlib import Path
26+
2127

2228
project = "Qiskit"
2329
project_copyright = f"2017-{datetime.date.today().year}, Qiskit Development Team"
@@ -39,7 +45,7 @@
3945
"sphinx.ext.intersphinx",
4046
"sphinx.ext.doctest",
4147
# This is used by qiskit/documentation to generate links to github.com.
42-
"sphinx.ext.viewcode",
48+
"sphinx.ext.linkcode",
4349
"matplotlib.sphinxext.plot_directive",
4450
"reno.sphinxext",
4551
"sphinxcontrib.katex",
@@ -155,3 +161,54 @@
155161
# ----------------------------------------------------------------------------------
156162

157163
plot_html_show_formats = False
164+
165+
166+
# ----------------------------------------------------------------------------------
167+
# Source code links
168+
# ----------------------------------------------------------------------------------
169+
170+
REPO_ROOT = Path(__file__).resolve().parents[1]
171+
172+
173+
def linkcode_resolve(domain, info):
174+
if domain != "py":
175+
return None
176+
177+
module_name = info["module"]
178+
if "qiskit" not in module_name:
179+
return None
180+
181+
try:
182+
module = importlib.import_module(module_name)
183+
except ModuleNotFoundError:
184+
return None
185+
186+
obj = module
187+
for part in info["fullname"].split("."):
188+
try:
189+
obj = getattr(obj, part)
190+
except AttributeError:
191+
return None
192+
193+
try:
194+
full_file_name = inspect.getsourcefile(obj)
195+
except TypeError:
196+
return None
197+
if full_file_name is None:
198+
return None
199+
try:
200+
relative_file_name = Path(full_file_name).resolve().relative_to(REPO_ROOT)
201+
file_name = re.sub(r"\.tox\/.+\/site-packages\/", "", str(relative_file_name))
202+
except ValueError:
203+
return None
204+
205+
try:
206+
source, lineno = inspect.getsourcelines(obj)
207+
except (OSError, TypeError):
208+
linespec = ""
209+
else:
210+
ending_lineno = lineno + len(source) - 1
211+
linespec = f"#L{lineno}-L{ending_lineno}"
212+
213+
github_branch = os.environ.get("QISKIT_DOCS_GITHUB_BRANCH_NAME", "main")
214+
return f"https://github.com/Qiskit/qiskit/tree/{github_branch}/{file_name}{linespec}"

tox.ini

+9-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@ setenv =
1515
QISKIT_SUPRESS_PACKAGING_WARNINGS=Y
1616
QISKIT_TEST_CAPTURE_STREAMS=1
1717
QISKIT_PARALLEL=FALSE
18-
passenv = RAYON_NUM_THREADS, OMP_NUM_THREADS, QISKIT_PARALLEL, RUST_BACKTRACE, SETUPTOOLS_ENABLE_FEATURES, QISKIT_TESTS, QISKIT_IN_PARALLEL
18+
passenv =
19+
RAYON_NUM_THREADS
20+
OMP_NUM_THREADS
21+
QISKIT_PARALLEL
22+
RUST_BACKTRACE
23+
SETUPTOOLS_ENABLE_FEATURES
24+
QISKIT_TESTS
25+
QISKIT_IN_PARALLEL
26+
QISKIT_DOCS_GITHUB_BRANCH_NAME
1927
deps =
2028
setuptools_rust # This is work around for the bug of tox 3 (see #8606 for more details.)
2129
-r{toxinidir}/requirements.txt

0 commit comments

Comments
 (0)