简体   繁体   English

将 Sphinx 与 reStructuredText 格式的文档字符串一起使用

[英]Utilizing Sphinx with reStructuredText formatted docstrings

According to the writing docstrings tutorial of Sphinx, it is possible to utilize Sphinx's autodoc extension to automatically generate documentation.根据Sphinx的writing docstrings tutorial ,可以利用Sphinx的autodoc扩展来自动生成文档。 We can either write docstring with the Sphinx format, Google or Numpy (the latter two with the napoleon extension).我们可以使用Sphinx格式编写文档字符串, GoogleNumpy (后两者带有napoleon扩展名)。

Is it possible to write docstrings in reStructuredText format?是否可以用 reStructuredText 格式编写文档字符串?

eg:例如:

"""[Summary]

Extended description of function.

:param int arg1: Description of arg1.
:param str arg2: Description of arg2.
:raise: ValueError if arg1 is equal to arg2
:return: Description of return value
:rtype: bool
"""

For comparison, this is Sphinx native format:为了比较,这是 Sphinx 原生格式:

"""[Summary]

:param [ParamName]: [ParamDescription], defaults to [DefaultParamVal]
:type [ParamName]: [ParamType](, optional)
...
:raises [ErrorType]: [ErrorDescription]
...
:return: [ReturnDescription]
:rtype: [ReturnType]
"""

Comparison of docstrings 文档字符串的比较

The two formats are actually the same.这两种格式其实是一样的。 This can be confusing but what's called the Info field lists can be considered the reST docstring syntax.这可能会造成混淆,但所谓的信息字段列表可以被认为是 reST 文档字符串语法。 If you look carefully at the version number it's been around since Sphinx version 0.4, next if we look at the current Sphinx change list it remits to a change list that predates version 1.0 ... The earliest mention there is:如果你仔细查看自 Sphinx 0.4 版以来的版本号,接下来如果我们查看当前的 Sphinx 更改列表,它会提交到早于 1.0 版的更改列表......最早提到的是:

Release 0.4 (Jun 23, 2008) 0.4 版(2008 年 6 月 23 日)

========================== ==========================

  • Sphinx now interprets field lists with fields like :param foo: in description units. Sphinx 现在以描述单元解释字段列表,其中包含:param foo:等字段。

If we want to dig further back to the definition of the reST docstring syntax the archives of the Doc-SIG - Python Documentation Special Interest Group would be the way to go, but a good enough overview is given by PEP 256 - Rationale dated 01-Jun-2001.如果我们想进一步挖掘 reST 文档字符串语法的定义, Doc-SIG 的档案 - Python 文档特别兴趣小组将是通往 go 的方式,但是PEP 256 - Rationale dated 01- 给出了足够好的概述2001 年 6 月。 The document that emerged from then and is most frequently cited only makes a loose recommendation:从那时起出现并被最常引用的文件只提出了一个松散的建议:

PEP 257 -- Docstring Conventions PEP 257——文档字符串约定

Python is case sensitive and the argument names can be used for keyword arguments, so the docstring should document the correct argument names. Python 区分大小写,参数名称可用于关键字 arguments,因此文档字符串应记录正确的参数名称。 It is best to list each argument on a separate line.最好在单独的行中列出每个参数。

To summarize things, the reST docstring syntax consists simply of using reST Field Lists !总而言之, reST 文档字符串语法仅包含使用reST 字段列表 (the NumPy and Google styles are just different styles of also writing reST Field Lists)! (NumPy 和谷歌 styles 只是不同的 styles 也写 reST 字段列表)!

Field List - reStructuredText Markup Specification 字段列表 - reStructuredText 标记规范

Field lists are mappings from field names to field bodies,字段列表是从字段名到字段体的映射,

(...) (...)

The interpretation of individual words in a multi-word field name is up to the application.多词字段名称中单个词的解释取决于应用程序。 The application may specify a syntax for the field name.应用程序可以指定字段名称的语法。

Syntax diagram (simplified):语法图(简化):

 +--------------------+----------------------+ | ":" field name ":" | field body | +-------+------------+ | | (body elements)+ | +-----------------------------------+

It's up to the application to specify the syntax of the field names;由应用程序指定字段名称的语法; so what Sphinx documentation generator specifies for the 2 example syntaxes in the question is that they are equivalent (this does not necessarily hold if you change to a different documentation generator).所以 Sphinx 文档生成器为问题中的 2 个示例语法指定的是它们是等效的(如果您更改为不同的文档生成器,这不一定成立)。

Thanks to @mzjin's answer in the comments: this link describes that it is possible since v0.4 .感谢@mzjin 在评论中的回答: 这个链接描述了从v0.4开始是可能的。

The below example is given in the link, which is exactly what I was looking for.下面的示例在链接中给出,这正是我正在寻找的。

py:function:: send_message(sender, recipient, message_body, [priority=1])
   """
   Send a message to a recipient

   :param str sender: The person sending the message
   :param str recipient: The recipient of the message
   :param str message_body: The body of the message
   :param priority: The priority of the message, can be a number 1-5
   :type priority: integer or None
   :return: the message id
   :rtype: int
   :raises ValueError: if the message_body exceeds 160 characters
   :raises TypeError: if the message_body is not a basestring
   """

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM