如何在 Sphinx 中交叉引用由 autodoc 生成的函数?

我使用 狮身人面像 autodoc特性基于 Python 库的 docstring 生成文档。

交叉引用的语法可以找到 给你

为了允许从文档的其他区域引用该节,该节前必须有一个标签。

我有一个. rst (RestructeredText)文件,用于我的一个类

.. autoclass:: classname
:members:

为类生成文档。

我的问题是,如何从另一个类引用自动生成的方法。第一份文件?如果我试图在方法的 docstring 中放置一个标签,Sphinx 会抱怨。如果我试图在方法标题之前放置一个标签,Sphinx 不能识别它。

有没有一种简单的方法可以做到这一点,或者我必须在类文件中显式地写入方法名,并在方法名前面加上一个标签?

下面是一个示例,在[ Python document entation2中的引用正在做我需要做的事情(我假设它使用了 autodoc 特性,尽管我不确定)

34952 次浏览

You don't need to add labels. In order to refer to a Python class, method, or other documented object, use the markup provided by the Python domain.

For example, the following defines a cross-reference to the mymethod method:

:py:meth:`mymodule.MyClass.mymethod`

Or even simpler (since the Python domain is the default):

:meth:`mymodule.MyClass.mymethod`

The documentation of TextWrapper.wrap that you link to in the question includes two cross-references of this kind (click on "Show Source" to see the reST markup).

In addition to the excellent answer already provided:

To add an alias to the referenced module (method, function, attribute, etc.), the following syntax is used:

:mod:`Alias Name <package.module>`

This will appear as a reference to Alias Name in the docs, and link to the module provided.