提交 cb86bd63 authored 作者: Ricardo Vieira's avatar Ricardo Vieira 提交者: Ricardo Vieira

Fix docs source linking

上级 afb0e1c1
import os import os
import inspect import inspect
import sys import sys
import pytensor import pytensor
from pathlib import Path from pathlib import Path
...@@ -234,24 +235,41 @@ htmlhelp_basename = "pytensor_doc" ...@@ -234,24 +235,41 @@ htmlhelp_basename = "pytensor_doc"
# Resolve function # Resolve function
# This function is used to populate the (source) links in the API # This function is used to populate the (source) links in the API
def linkcode_resolve(domain, info): def linkcode_resolve(domain, info):
def find_source(): def find_obj() -> object:
# try to find the file and line number, based on code from numpy: # try to find the file and line number, based on code from numpy:
# https://github.com/numpy/numpy/blob/master/doc/source/conf.py#L286 # https://github.com/numpy/numpy/blob/master/doc/source/conf.py#L286
obj = sys.modules[info["module"]] obj = sys.modules[info["module"]]
for part in info["fullname"].split("."): for part in info["fullname"].split("."):
obj = getattr(obj, part) obj = getattr(obj, part)
return obj
def find_source(obj):
fn = Path(inspect.getsourcefile(obj)) fn = Path(inspect.getsourcefile(obj))
fn = fn.relative_to(Path(__file__).parent) fn = fn.relative_to(Path(pytensor.__file__).parent)
source, lineno = inspect.getsourcelines(obj) source, lineno = inspect.getsourcelines(obj)
return fn, lineno, lineno + len(source) - 1 return fn, lineno, lineno + len(source) - 1
def fallback_source():
return info["module"].replace(".", "/") + ".py"
if domain != "py" or not info["module"]: if domain != "py" or not info["module"]:
return None return None
try:
obj = find_obj()
except Exception:
filename = fallback_source()
else:
try: try:
filename = "pytensor/%s#L%d-L%d" % find_source() filename = "pytensor/%s#L%d-L%d" % find_source(obj)
except Exception: except Exception:
filename = info["module"].replace(".", "/") + ".py" # warnings.warn(f"Could not find source code for {domain}:{info}")
try:
filename = obj.__module__.replace(".", "/") + ".py"
except AttributeError:
# Some objects do not have a __module__ attribute (?)
filename = fallback_source()
import subprocess import subprocess
tag = subprocess.Popen( tag = subprocess.Popen(
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论