Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use sphinx.ext.linkcode for more precise source code links #11851

Merged
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
099636d
switched from sphinx.ext.viewcode to sphinx.ext.linkcode
melechlapson Feb 20, 2024
00b05e7
removed extra line
melechlapson Feb 21, 2024
a20ad5c
Merge branch 'main' into mlapson/switch-to-sphinx-ext-linkcode
melechlapson Feb 21, 2024
2f94288
Add section header for source code links
melechlapson Feb 21, 2024
1d5c34d
removed docstring
melechlapson Feb 21, 2024
1849ce1
update return string
melechlapson Feb 21, 2024
cca9b04
added back blank line
melechlapson Feb 21, 2024
5bd18cd
Merge branch 'mlapson/switch-to-sphinx-ext-linkcode' of https://githu…
melechlapson Feb 21, 2024
b985ba6
Added a method to determine the GitHub branch
melechlapson Feb 21, 2024
914d2cd
Merge branch 'main' into mlapson/switch-to-sphinx-ext-linkcode
melechlapson Feb 21, 2024
1deb939
add blank line
melechlapson Feb 21, 2024
b84b399
remove print statement
melechlapson Feb 21, 2024
01e4ca6
Try to fix error for contextlib file
Eric-Arellano Feb 21, 2024
25ff4df
Try to fix error for Jenkins run #20240221.52
melechlapson Feb 21, 2024
ace069b
Check that qiskit in module name sooner
Eric-Arellano Feb 21, 2024
5420982
moved valid code object verification earlier
melechlapson Feb 27, 2024
d60bbf4
Merge branch 'main' into mlapson/switch-to-sphinx-ext-linkcode
melechlapson Feb 27, 2024
17881d4
added try except statement to getattr call
melechlapson Feb 27, 2024
9330e45
added extra try/except block
melechlapson Feb 27, 2024
ab8dad0
Also support Azure Pipelines
Eric-Arellano Feb 27, 2024
6ef1f68
removed unused import
melechlapson Feb 27, 2024
b45113b
Revert Azure support to keep things simple
Eric-Arellano Feb 27, 2024
a3e5297
added extra "/" to final URL
melechlapson Feb 27, 2024
e3382e7
Merge branch 'mlapson/switch-to-sphinx-ext-linkcode' of https://githu…
melechlapson Feb 27, 2024
825e02a
Merge branch 'main' into mlapson/switch-to-sphinx-ext-linkcode
melechlapson Feb 28, 2024
51f4bcb
Merge branch 'main' of github.com:Qiskit/qiskit-terra into mlapson/sw…
Eric-Arellano Mar 7, 2024
ab7f411
Move GitHub branch logic to GitHub Action
Eric-Arellano Mar 7, 2024
2a28edb
Merge branch 'mlapson/switch-to-sphinx-ext-linkcode' of github.com:me…
Eric-Arellano Mar 7, 2024
cde30ef
switched to importlib and removed redundant block of code
melechlapson Mar 20, 2024
5545bc8
Apply suggestions from code review
melechlapson Mar 21, 2024
7c95384
Merge branch 'main' into mlapson/switch-to-sphinx-ext-linkcode
melechlapson Mar 21, 2024
5b74a03
added back spaces
melechlapson Apr 4, 2024
8a805da
Merge branch 'main' of github.com:Qiskit/qiskit-terra into mlapson/sw…
Eric-Arellano Apr 4, 2024
abf4871
Clarify docs_deploy GitHub logic
Eric-Arellano Apr 4, 2024
3386c4c
Use pathlib for relativizing file name
Eric-Arellano Apr 4, 2024
614e8f8
Fix relative_to() path
Eric-Arellano Apr 5, 2024
19d052d
Remove tox prefix
Eric-Arellano Apr 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion .github/workflows/docs_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,21 @@ jobs:
- name: Install dependencies
run: tools/install_ubuntu_docs_dependencies.sh

- name: Determine GitHub branch name
run: |
# Tags like 1.0.0 and 1.0.0rc1 should point to their stable branch. We do this
# to reduce the diff in the qiskit/documentation repository between generating
# the API docs from a tag release versus a workflow_dispatch.
if [[ $GITHUB_REF_NAME =~ ^([0-9]+\.[0-9]+) ]]; then
BRANCH_NAME="stable/${BASH_REMATCH[1]}"
else
BRANCH_NAME="$GITHUB_REF_NAME"
fi
echo "Using branch '${BRANCH_NAME}' for GitHub source code links"
echo "QISKIT_DOCS_GITHUB_BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV

- name: Build documentation
run: tox run -e docs

- name: Store built documentation artifact
uses: actions/upload-artifact@v4
with:
Expand Down
57 changes: 56 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@

melechlapson marked this conversation as resolved.
Show resolved Hide resolved
import datetime
import doctest
import importlib
import inspect
import os
from pathlib import Path


project = "Qiskit"
project_copyright = f"2017-{datetime.date.today().year}, Qiskit Development Team"
Expand All @@ -39,7 +44,7 @@
"sphinx.ext.intersphinx",
"sphinx.ext.doctest",
# This is used by qiskit/documentation to generate links to github.com.
"sphinx.ext.viewcode",
"sphinx.ext.linkcode",
"matplotlib.sphinxext.plot_directive",
"reno.sphinxext",
"sphinxcontrib.katex",
Expand Down Expand Up @@ -155,3 +160,53 @@
# ----------------------------------------------------------------------------------

plot_html_show_formats = False


# ----------------------------------------------------------------------------------
# Source code links
# ----------------------------------------------------------------------------------

REPO_ROOT = Path(__file__).resolve().parents[1]


def linkcode_resolve(domain, info):
if domain != "py":
return None

module_name = info["module"]
if "qiskit" not in module_name:
return None

try:
module = importlib.import_module(module_name)
except ModuleNotFoundError:
return None

obj = module
for part in info["fullname"].split("."):
try:
obj = getattr(obj, part)
except AttributeError:
return None

try:
full_file_name = inspect.getsourcefile(obj)
except TypeError:
return None
if full_file_name is None:
return None
try:
file_name = Path(full_file_name).resolve().relative_to(REPO_ROOT / "qiskit")
except ValueError:
return None

try:
source, lineno = inspect.getsourcelines(obj)
except (OSError, TypeError):
linespec = ""
else:
ending_lineno = lineno + len(source) - 1
linespec = f"#L{lineno}-L{ending_lineno}"

github_branch = os.environ.get("QISKIT_DOCS_GITHUB_BRANCH_NAME", "main")
return f"https://github.com/Qiskit/qiskit/tree/{github_branch}/qiskit/{file_name}{linespec}"
10 changes: 9 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ setenv =
QISKIT_SUPRESS_PACKAGING_WARNINGS=Y
QISKIT_TEST_CAPTURE_STREAMS=1
QISKIT_PARALLEL=FALSE
passenv = RAYON_NUM_THREADS, OMP_NUM_THREADS, QISKIT_PARALLEL, RUST_BACKTRACE, SETUPTOOLS_ENABLE_FEATURES, QISKIT_TESTS, QISKIT_IN_PARALLEL
passenv =
RAYON_NUM_THREADS
OMP_NUM_THREADS
QISKIT_PARALLEL
RUST_BACKTRACE
SETUPTOOLS_ENABLE_FEATURES
QISKIT_TESTS
QISKIT_IN_PARALLEL
QISKIT_DOCS_GITHUB_BRANCH_NAME
deps =
setuptools_rust # This is work around for the bug of tox 3 (see #8606 for more details.)
-r{toxinidir}/requirements.txt
Expand Down
Loading