Skip to content

Commit f876f97

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

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

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

247+
def find_source(obj):
245248
fn = Path(inspect.getsourcefile(obj))
246-
fn = fn.relative_to(Path(__file__).parent)
249+
fn = fn.relative_to(Path(pytensor.__file__).parent)
247250
source, lineno = inspect.getsourcelines(obj)
248251
return fn, lineno, lineno + len(source) - 1
249252

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

258276
tag = subprocess.Popen(

0 commit comments

Comments
 (0)