diff --git a/docs/source/conf.py b/docs/source/conf.py index cea2a550f8..16798fd9e6 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -14,6 +14,8 @@ from __future__ import annotations import importlib +import inspect +import os import re import sys from inspect import getmembers, isclass, isfunction @@ -21,6 +23,7 @@ from click import secho, style +import kedro from kedro import __version__ as release # -- Project information ----------------------------------------------------- @@ -47,7 +50,7 @@ "sphinx_autodoc_typehints", "sphinx.ext.doctest", "sphinx.ext.ifconfig", - "sphinx.ext.viewcode", + "sphinx.ext.linkcode", "sphinx_copybutton", "myst_parser", "notfound.extension", @@ -534,3 +537,26 @@ def setup(app): myst_heading_anchors = 5 myst_enable_extensions = ["colon_fence"] + +def linkcode_resolve(domain, info): + """Resolve a GitHub URL corresponding to a Python object.""" + if domain != 'py': + return None + + try: + mod = sys.modules[info['module']] + obj = mod + for attr in info['fullname'].split('.'): + obj = getattr(obj, attr) + obj = inspect.unwrap(obj) + + filename = inspect.getsourcefile(obj) + source, lineno = inspect.getsourcelines(obj) + relpath = os.path.relpath(filename, start=os.path.dirname( + kedro.__file__)) + + return 'https://github.com/kedro-org/kedro/blob/main/kedro/%s#L%d#L%d' % ( + relpath, lineno, lineno + len(source) - 1 + ) + except (KeyError, ImportError, AttributeError, TypeError, OSError, ValueError): + return None