sphinx 超链接到在别名下导入的 Python 模块的成员



我维护一个 Python 包,通常以import numpy as npimport pandas as pd的方式使用简短的标准化昵称导入。 假设它是import foobar as fb.

我正在使用狮身人面像来记录它,在我的conf.py中,我'sphinx.ext.autodoc'在我的extensions中,default_role = 'py:obj'。 这是一个很好的设置,因为这意味着每当我的文档字符串或单独的.rst文件包含整洁的字符串时,例如

See the `foobar.Thing` class documentation for more details. 

甚至只是

See the `Thing` class documentation for more details.

然后,反引号中的文本会自动超链接到foobar.Thing类的文档。 但缺少的是做到这一点的能力:

See the `fb.Thing` class documentation for more details.

文本fb.Thing不会超链接,因为 sphinx(或 sphinx autodoc(不知道fbfoobar包的别名。 我怎么知道是这种情况?

注意:我知道可以用<>符号来做到这一点:

See the `fb.Thing <foobar.Thing>` class documentation for more details.

但是文档字符串也被设计为纯文本阅读,所以我希望这可以在不引入这种或其他形式的:clutter:`...`的情况下完成,而是以某种方式在conf.py文件或.. automodule::语句中。

这可以通过狮身人面像来解决。如果在本地对象清单中找不到名称,则可以在外部源中进行搜索。您可以将自己的文档添加为狮身人面像清单。

狮身人面像配置:

# ==============================================================================
# Sphinx.Ext.InterSphinx
# ==============================================================================
intersphinx_mapping = {
#  'python':       ('https://docs.python.org/3', None),
'foobar': ('http://foobar.readthedocs.io/en/latest', None),
}

用法:

At next, I want to document `fb.Thing <foobar:Thing>`, because it's a great implementation.

更多资源:

  • 狮身人面像文档:https://www.sphinx-doc.org/en/master/usage/extensions/intersphinx.html
  • 有时,狮身人面像
  • 可以处理别名:覆盖狮身人面像autodoc"别名"以导入私有类?

我不知道如何让狮身人面像知道模块别名,但这可能接近您想要的符号:

somefile.py

"""
Some documentation with references to an externally documented function
:func:`numpy.searchsorted` or :func:`partial.update_wrapper`
"""

conf.py

# intersphinx settings
intersphinx_mapping = {'numpy': ('https://docs.scipy.org/doc/numpy', None),
'functools': ('https://docs.python.org/3.7/library/functools.html', None)}

最新更新