From 004e7e1c0a1542cb6b1e33a68ea19eca5d00a0cc Mon Sep 17 00:00:00 2001 From: Melech Lapson Date: Tue, 27 Feb 2024 11:35:49 -0600 Subject: [PATCH 1/5] switched from using sphinx-ext-viewcode to sphinx-ext-linkcode --- docs/conf.py | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index aa6b69fa..90a968b8 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,85 @@ 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 + and "BUILD_SOURCE_BRANCH_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") + or os.environ.get("SYSTEM_PULL_REQUEST_TARGET_BRANCH_NAME") + ): + return base_ref + + ref_name = ( + os.environ.get("GITHUB_REF_NAME") + or os.environ.get("BUILD_SOURCE_BRANCH_NAME") + ) + assert ref_name is not None + + # 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_match = re.match(r"(\d+\.\d+)", ref_name) + return ( + f"stable/{version_without_patch_match.group()}" + if version_without_patch_match + 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" 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: + 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}" From 4e17a5f12f0627e52a7bdb59a5b33e4d1ef6bec7 Mon Sep 17 00:00:00 2001 From: Melech Lapson Date: Tue, 27 Feb 2024 11:51:43 -0600 Subject: [PATCH 2/5] updated tox.ini --- tox.ini | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tox.ini b/tox.ini index 7733f6c0..b602c4f0 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 From 832b61f5bf042cef2062fb76e9d85859d7cedda9 Mon Sep 17 00:00:00 2001 From: Melech Lapson Date: Tue, 27 Feb 2024 11:57:57 -0600 Subject: [PATCH 3/5] removed leftover code from azure --- docs/conf.py | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 90a968b8..b468a139 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -117,35 +117,24 @@ def determine_github_branch() -> str: 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 - and "BUILD_SOURCE_BRANCH_NAME" not in os.environ - ): + 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") - or os.environ.get("SYSTEM_PULL_REQUEST_TARGET_BRANCH_NAME") - ): + if base_ref := os.environ.get("GITHUB_BASE_REF"): return base_ref - ref_name = ( - os.environ.get("GITHUB_REF_NAME") - or os.environ.get("BUILD_SOURCE_BRANCH_NAME") - ) - assert ref_name is not None + 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_match = re.match(r"(\d+\.\d+)", ref_name) + version_without_patch = re.match(r"(\d+\.\d+)", ref_name) return ( - f"stable/{version_without_patch_match.group()}" - if version_without_patch_match + f"stable/{version_without_patch.group()}" + if version_without_patch else ref_name ) - GITHUB_BRANCH = determine_github_branch() From 52214639205d2a0cad78f5d18eaf03af259666ef Mon Sep 17 00:00:00 2001 From: Melech Lapson Date: Tue, 27 Feb 2024 11:58:53 -0600 Subject: [PATCH 4/5] switched dashes to underscores --- docs/conf.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index b468a139..55519e54 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -135,6 +135,7 @@ def determine_github_branch() -> str: else ref_name ) + GITHUB_BRANCH = determine_github_branch() @@ -144,7 +145,7 @@ def linkcode_resolve(domain, info): module_name = info["module"] module = sys.modules.get(module_name) - if module is None or "qiskit" not in module_name: + if module is None or "qiskit_ibm_provider" not in module_name: return None obj = module @@ -164,7 +165,7 @@ def linkcode_resolve(domain, info): return None if full_file_name is None: return None - file_name = full_file_name.split("/qiskit-ibm-provider/")[-1] + file_name = full_file_name.split("/qiskit_ibm_provider/")[-1] try: source, lineno = inspect.getsourcelines(obj) @@ -173,4 +174,4 @@ def linkcode_resolve(domain, info): 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}" + return f"https://github.com/Qiskit/qiskit-ibm-provider/tree/{GITHUB_BRANCH}/qiskit_ibm_provider/{file_name}{linespec}" From 88a927f74542793d153e316ca594ba6c45fb7475 Mon Sep 17 00:00:00 2001 From: Eric Arellano <14852634+Eric-Arellano@users.noreply.github.com> Date: Tue, 27 Feb 2024 13:45:43 -0500 Subject: [PATCH 5/5] Try fix for methods inherited by Qiskit --- docs/conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index 55519e54..04cb2949 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -163,7 +163,7 @@ def linkcode_resolve(domain, info): full_file_name = inspect.getsourcefile(obj) except TypeError: return None - if full_file_name is 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]