【问题标题】:reStructuredText link in PythonPython中的reStructuredText链接
【发布时间】:2016-12-21 10:42:56
【问题描述】:

如果我在 Python 中有以下函数:

def _add_parameter(self, a):
    # do something

我想添加一个文档字符串以使用 reStructuredText 链接到上述函数,我只需要在函数名称的末尾添加一个下划线,如下所示:

"""
The _add_parameter_ adds parameters.
"""

但我没有将其链接到函数,而是收到cannot find declaration to go to 警告。我该如何解决这个问题?

【问题讨论】:

  • 您在使用 Sphinx 吗?那就看sphinx-doc.org/en/stable/domains.html
  • 是的,我正在使用 Sphinx。所以根据你的链接我应该做 :py:func:_add_parameter ?
  • 这是一种方法,所以可能是:py:meth:~ClassName.method_name,但是去看看有什么用

标签: python python-2.7 python-3.x restructuredtext docstring


【解决方案1】:

autodoc 扩展仅记录非私有成员,即名称不以下划线开头的成员。来自文档:

.. autoclass:: Noodle
       :members:
    will document all non-private member functions and properties (that is,
    those whose name doesn’t start with _).

因此,当 autodoc 尝试查找要链接的位置时,它没有找到。

要覆盖不记录私有成员,您可以使用:private-members:,但我无法根据经验判断这是否有效。但是,通常最好只记录公共接口。

【讨论】:

    猜你喜欢
    • 2012-12-14
    • 2015-09-10
    • 2013-02-10
    • 2015-09-11
    • 2021-08-01
    • 2011-04-07
    • 2011-06-12
    • 2012-07-27
    相关资源
    最近更新 更多