From c25415da2cc50389b0b0f26b0f9c5f9ad4399c4d Mon Sep 17 00:00:00 2001 From: melechlapson Date: Wed, 28 Feb 2024 09:06:28 -0600 Subject: [PATCH] Use sphinx.ext.linkcode for more precise source code links (#821) * switched from using sphinx-ext-viewcode to sphinx-ext-linkcode * updated tox.ini * removed leftover code from azure * switched dashes to underscores * Try fix for methods inherited by Qiskit --------- Co-authored-by: Eric Arellano <14852634+Eric-Arellano@users.noreply.github.com> --- docs/conf.py | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++- tox.ini | 3 ++ 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index daae9be47..18a729ce1 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -10,6 +10,10 @@ # copyright notice, and modified files need to carry a notice indicating # that they have been altered from the originals. +import inspect +import os +import re +import sys # -- Project information ----------------------------------------------------- project = 'Qiskit IBM Quantum Provider' @@ -27,7 +31,7 @@ 'sphinx.ext.napoleon', 'sphinx.ext.autodoc', 'sphinx.ext.autosummary', - 'sphinx.ext.viewcode', + 'sphinx.ext.linkcode', 'sphinx.ext.extlinks', 'sphinx.ext.intersphinx', 'jupyter_sphinx', @@ -99,3 +103,75 @@ html_last_updated_fmt = '%Y/%m/%d' html_sourcelink_suffix = '' + + +# ---------------------------------------------------------------------------------- +# Source code links +# ---------------------------------------------------------------------------------- + +def determine_github_branch() -> str: + """Determine the GitHub branch name to use for source code links. + + We need to decide whether to use `stable/` vs. `main` for dev builds. + Refer to https://docs.github.com/en/actions/learn-github-actions/variables + for how we determine this with GitHub Actions. + """ + # If CI env vars not set, default to `main`. This is relevant for local builds. + if "GITHUB_REF_NAME" not in os.environ: + return "main" + + # PR workflows set the branch they're merging into. + if base_ref := os.environ.get("GITHUB_BASE_REF"): + return base_ref + + ref_name = os.environ["GITHUB_REF_NAME"] + + # Check if the ref_name is a tag like `1.0.0` or `1.0.0rc1`. If so, we need + # to transform it to a Git branch like `stable/1.0`. + version_without_patch = re.match(r"(\d+\.\d+)", ref_name) + return ( + f"stable/{version_without_patch.group()}" + if version_without_patch + else ref_name + ) + + +GITHUB_BRANCH = determine_github_branch() + + +def linkcode_resolve(domain, info): + if domain != "py": + return None + + module_name = info["module"] + module = sys.modules.get(module_name) + if module is None or "qiskit_ibm_provider" not in module_name: + return None + + obj = module + for part in info["fullname"].split("."): + try: + obj = getattr(obj, part) + except AttributeError: + return None + is_valid_code_object = ( + inspect.isclass(obj) or inspect.ismethod(obj) or inspect.isfunction(obj) + ) + if not is_valid_code_object: + return None + try: + full_file_name = inspect.getsourcefile(obj) + except TypeError: + return None + if full_file_name is None or "/qiskit_ibm_provider/" not in full_file_name: + return None + file_name = full_file_name.split("/qiskit_ibm_provider/")[-1] + + try: + source, lineno = inspect.getsourcelines(obj) + except (OSError, TypeError): + linespec = "" + else: + ending_lineno = lineno + len(source) - 1 + linespec = f"#L{lineno}-L{ending_lineno}" + return f"https://github.com/Qiskit/qiskit-ibm-provider/tree/{GITHUB_BRANCH}/qiskit_ibm_provider/{file_name}{linespec}" diff --git a/tox.ini b/tox.ini index 7733f6c00..b602c4f00 100644 --- a/tox.ini +++ b/tox.ini @@ -10,6 +10,9 @@ setenv = VIRTUAL_ENV={envdir} LANGUAGE=en_US LC_ALL=en_US.utf-8 +passenv= + GITHUB_REF_NAME, + GITHUB_BASE_REF commands = python -m unittest -v