我如何使用 Sphinx 引用其他文件中的 类 和方法?

How do I refer to classes and methods in other files my project with Sphinx?

我正在尝试将 link 的 Sphinx 文档字符串编写到包的其他部分的一些 classes 和方法中,但无法弄清楚如何构建这些 links 或让它们在我需要时显示。

我有

# a.py

from .b import *

class A(object):

    def func_one():
        """Does stuff using `func_two` from `B`."""
        some_b = B ...
        some_b.func_two()
        # ...

# b.py

class B(object):

    def func_two():
        # ...

我的包裹存放在哪里

my_package/
    a.py
    b.py

我希望 A.func_one 的 Sphinx 文档显示为

Does stuff using func_two from B.

并包含 link 至 func_twoB

我已经尝试了方法全名和 class 的各种组合,但似乎都不起作用。我该如何实现?

要创建对 Python 对象的交叉引用,请使用 Python 域提供的角色(:class::meth: 等)。参见 https://www.sphinx-doc.org/en/master/usage/restructuredtext/domains.html#cross-referencing-python-objects

示例:

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

要使 link 文本仅成为目标的最后一部分,请使用 ~(波浪号)前缀。参见 https://www.sphinx-doc.org/en/master/usage/restructuredtext/domains.html#cross-referencing-syntax

示例:

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