Skip to content

Commit cb86bd6

Browse files
Fix docs source linking
Co-authored-by: Oriol Abril-Pla <oriol.abril.pla@gmail.com>
1 parent afb0e1c commit cb86bd6

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

doc/conf.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
import inspect
33
import sys
4+
45
import pytensor
56
from pathlib import Path
67

@@ -234,24 +235,41 @@
234235
# Resolve function
235236
# This function is used to populate the (source) links in the API
236237
def linkcode_resolve(domain, info):
237-
def find_source():
238+
def find_obj() -> object:
238239
# try to find the file and line number, based on code from numpy:
239240
# https://github.com/numpy/numpy/blob/master/doc/source/conf.py#L286
240241
obj = sys.modules[info["module"]]
241242
for part in info["fullname"].split("."):
242243
obj = getattr(obj, part)
244+
return obj
243245

246+
def find_source(obj):
244247
fn = Path(inspect.getsourcefile(obj))
245-
fn = fn.relative_to(Path(__file__).parent)
248+
fn = fn.relative_to(Path(pytensor.__file__).parent)
246249
source, lineno = inspect.getsourcelines(obj)
247250
return fn, lineno, lineno + len(source) - 1
248251

252+
def fallback_source():
253+
return info["module"].replace(".", "/") + ".py"
254+
249255
if domain != "py" or not info["module"]:
250256
return None
257+
251258
try:
252-
filename = "pytensor/%s#L%d-L%d" % find_source()
259+
obj = find_obj()
253260
except Exception:
254-
filename = info["module"].replace(".", "/") + ".py"
261+
filename = fallback_source()
262+
else:
263+
try:
264+
filename = "pytensor/%s#L%d-L%d" % find_source(obj)
265+
except Exception:
266+
# warnings.warn(f"Could not find source code for {domain}:{info}")
267+
try:
268+
filename = obj.__module__.replace(".", "/") + ".py"
269+
except AttributeError:
270+
# Some objects do not have a __module__ attribute (?)
271+
filename = fallback_source()
272+
255273
import subprocess
256274

257275
tag = subprocess.Popen(

0 commit comments

Comments
 (0)