Skip to content

Commit 7c51388

Browse files
svekarsmalfet
authored andcommitted
[Docs] Make links to source link to source (pytorch#141186)
Rewrite [SOURCE] links in the API docs to point to the source file in github repo. Pull Request resolved: pytorch#141186 Approved by: https://github.com/malfet, https://github.com/msaroufim Co-authored-by: Nikita Shulga <[email protected]>
1 parent 9370d91 commit 7c51388

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

docs/source/conf.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
# All configuration values have a default; values that are commented out
1212
# serve to show the default.
1313

14+
import inspect
15+
1416
# If extensions (or modules to document with autodoc) are in another directory,
1517
# add these directories to sys.path here. If the directory is relative to the
1618
# documentation root, use os.path.abspath to make it absolute, like shown here.
@@ -62,6 +64,7 @@
6264
"sphinx_copybutton",
6365
"sphinx_panels",
6466
"myst_parser",
67+
"sphinx.ext.linkcode",
6568
]
6669

6770
# build the templated autosummary files
@@ -3378,6 +3381,45 @@
33783381
html_title = " ".join((project, version, "documentation"))
33793382
release = version
33803383

3384+
3385+
# Use the linkcode extension to override [SOURCE] links to point
3386+
# to the repo. Use the torch_version variable defined above to
3387+
# determine link
3388+
def linkcode_resolve(domain, info):
3389+
if domain != "py":
3390+
return None
3391+
if not info["module"]:
3392+
return None
3393+
3394+
try:
3395+
module = __import__(info["module"], fromlist=[""])
3396+
obj = module
3397+
for part in info["fullname"].split("."):
3398+
obj = getattr(obj, part)
3399+
# Get the source file and line number
3400+
obj = inspect.unwrap(obj)
3401+
fn = inspect.getsourcefile(obj)
3402+
source, lineno = inspect.getsourcelines(obj)
3403+
except Exception:
3404+
return None
3405+
3406+
# Determine the tag based on the torch_version
3407+
if RELEASE:
3408+
version_parts = torch_version.split(
3409+
"."
3410+
) # For release versions, format as "vX.Y.Z" for correct path in repo
3411+
patch_version = (
3412+
version_parts[2].split("+")[0].split("a")[0]
3413+
) # assuming a0 always comes after release version in versions.txt
3414+
version_path = f"v{version_parts[0]}.{version_parts[1]}.{patch_version}"
3415+
else:
3416+
version_path = torch.version.git_version
3417+
fn = os.path.relpath(fn, start=os.path.dirname(torch.__file__))
3418+
return (
3419+
f"https://github.com/pytorch/pytorch/blob/{version_path}/torch/{fn}#L{lineno}"
3420+
)
3421+
3422+
33813423
# The language for content autogenerated by Sphinx. Refer to documentation
33823424
# for a list of supported languages.
33833425
#

0 commit comments

Comments
 (0)