From a7064095c8f12341913d95144b6a78e73761678c Mon Sep 17 00:00:00 2001 From: Eric Arellano <14852634+Eric-Arellano@users.noreply.github.com> Date: Mon, 7 Oct 2024 13:11:55 -0400 Subject: [PATCH] Fix GitHub source code links for decorated functions (#1960) (#1961) --- docs/conf.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 6b067580f..e75d63c79 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -176,17 +176,26 @@ def linkcode_resolve(domain, info): if module is None or "qiskit_ibm_runtime" not in module_name: return None + def is_valid_code_object(obj): + return ( + inspect.isclass(obj) or inspect.ismethod(obj) or inspect.isfunction(obj) + ) + 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: + if not is_valid_code_object(obj): + return None + + # Unwrap decorators. This requires they used `functools.wrap()`. + while hasattr(obj, "__wrapped__"): + obj = getattr(obj, "__wrapped__") + if not is_valid_code_object(obj): return None + try: full_file_name = inspect.getsourcefile(obj) except TypeError: