- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的代码中,我有一些属于复杂枚举类型的类。例如:
class ComplexEnum(SomeOtherClass, Enum):
""" Some documentation """
MEMBER1 = SomeOtherClass(1)
MEMBER2 = SomeOtherClass(2)
def __init__(self, arg):
""" more doc """
pass
def somemethod(self):
""" more doc """
pass
@classmethod
def someclassmethod(cls, otherparam):
""" more doc """
pass
当我现在使用 autodoc 使用 Sphinx 创建文档时,会跳过此类。我尝试将这样的自定义文档添加到我的 conf.py 文件中:
from sphinx.ext.autodoc import ClassDocumenter
class MyClassDocumenter(ClassDocumenter):
objtype = 'ComplexEnum'
directivetype = 'class'
@classmethod
def can_document_member(cls, member, membername, isattr, parent):
return isinstance(member, ComplexEnum)
def setup(app):
app.add_autodocumenter(MyClassDocumenter)
但这也不起作用。
如何使 sphinx 记录此类类?
最佳答案
这是 Sphinx autodoc
中使用 Enum 时出现的错误。
可以通过仔细编写 .rst
文件来解决这个问题。
话虽如此,我认为这是为了:
对应的.rst
:
my_module module
================
.. automodule:: my_module
:exclude-members: ComplexEnum
.. autoclass:: ComplexEnum
:members: some_method
:show-inheritance:
:exclude-members: MEMBER1, MEMBER2, __init__, some_classmethod
.. automethod:: some_classmethod
.. autoattribute:: MEMBER1
:annotation: = SomeOtherClass(1)
.. autoattribute:: MEMBER2
:annotation: = SomeOtherClass(2)
.. automethod:: __init__
.. autoclass:: SomeOtherClass
:special-members: __init__
我稍微修改了代码以更好地解释解决方法的一些细节:
from enum import Enum
class SomeOtherClass:
""" SomeOtherClass documentation """
def __init__(self, other_arg):
"""Example of docstring on the __init__ method.
Args:
other_arg (int): Description of `other_arg`.
"""
self.other_arg = other_arg
class ComplexEnum(SomeOtherClass, Enum):
"""ComplexEnum documentation."""
#: :py:mod:`~my_package.my_module.SomeOtherClass`: MEMBER1 docstring comment.
MEMBER1 = SomeOtherClass(1)
#: :py:mod:`~my_package.my_module.SomeOtherClass`: MEMBER2 docstring comment.
MEMBER2 = SomeOtherClass(2)
def __init__(self, complex_arg):
"""Example of docstring on the __init__ method.
Args:
complex_arg (int): Description of `complex_arg`.
"""
self.complex_arg = complex_arg
super().__init__(complex_arg)
def some_method(self):
"""The doc of some_method."""
pass
@classmethod
def some_classmethod(cls, some_arg):
"""The doc of some_classmethod.
Args:
some_arg (int): Description of `some_arg`.
"""
pass
你的conf.py
可以保留标准,我只添加extensions = ['sphinx.ext.autodoc', 'sphinx.ext.napoleon']
来启用谷歌风格的评论。
到目前为止,在 @mzjn 贡献的链接和您的帖子中已经确定了触发该错误的具体条件组合,即:
应该注意的是:使用带有@classmethod的简单枚举不会触发该错误。 (在这种情况下,.. autoclass::
的行为符合预期,并且几乎处理所有事情。)
该错误会影响多个 autodoc
指令及其选项,导致它们出现意外行为。
编写 .rst 时必要的解决方法如下:
请勿在枚举中使用 :undoc-members:
,否则 caos 会崩溃。如果这样做,则始终包含 @classmethod 而不会获取描述符或文档字符串,并且使用 :exclude-members:
将其排除将无效。
接下来的__init__
是最有问题的方面。有效的方法是使用 :exclude-members:
排除它,并显式使用 .. automethod::__init__
。
与上述一起:您不能在 .rst
中使用 :automethod:
将 @classmethod 放在 __init__
旁边,否则整个 @classmethod 会被“吸收”为 __init__
文档字符串的一部分。
对我来说,最有效的方法是使用 :members:
和 :exclude-members:
显式包含/排除枚举的所有部分。这保证了 autodoc
指令/选项的行为的最佳一致性。
两个最后的注释与使用 Sphinx 记录 Enum 相关(与错误没有直接关系)。
在记录 Enum 成员时,为了获得最佳一致性,请使用 #:
语法而不是三引号 '''
或内联 #
。原因是,因为后者经常被Sphinx“混淆”甚至丢失。
..member-order: by source
作为指令选项或在配置中,上述情况通常也是如此。最后,如果您希望枚举成员的值显示在文档中,就像它们出现在类声明语法中一样。根据我的经验,最好的方法是使用 :annotation:
,如 .rst
中所示。否则,枚举成员将显示在如下文档中:
将 Python 3.8 与 Sphinx v2.2.2 结合使用。
关于python - Sphinx 没有记录复杂的 Enum 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59611074/
在 Sphinx 2.0.6 中尝试启用通配符 (*) 的搜索时出现以下错误 index products: syntax error, unexpected $undefined near '*'
如果我更新 sphinx.conf 文件中的资源,我可以使用 --rotate 重新索引,一切正常。如果我更新 sphinx.conf 中的索引或添加新索引 --rotate 无效,我必须重新启动 s
问题 我一直在用(Python) Sphinx doc ,以及 CommonMark解析器,编写包含用 reStructuredText 和 Markdown 编写的文件的 Sphinx 文档。到目前
我正在使用漂亮的 sphinx-bootstrap-theme 0.3.4 并尝试将它应用到 Sphinx Python 文档生成器 1.2 版之上。 当我通过 make html 构建文档时,我没有
关于此主题,有几篇“未答复”的帖子与无法找到“sphinx-build”有关: sphinx-build -h command not found in Mac OS Sphinx 是在 OSX 上使
我正在使用 Sphinx 搜索引擎,我遇到一个问题,即一些文件没有显示在搜索结果中,但绝对应该显示。我已经检查以确保没有信息。缺少会阻止这些文件出现的信息。 有什么方法可以直接查询索引,看看有没有这些
如何使用 Sphinx 从索引中获取所有记录?就像 SELECT * FROM index 一样?我知道我可以做这样的事情来获取与特定关键字匹配的所有记录:/usr/local/sphinx/bin/
我对 Sphinx 很陌生,在服务器上记录我的项目。现在一位同事看到了我一直在做的事情,她想做同样的事情——在同一台服务器上记录她的项目。 这些项目不相关(它们不属于单个 TOCtree),我不知道如
我有一个很大的索引定义,索引需要很长时间。我怀疑主要问题是由生成的许多 LEFT OUTER JOIN 引起的。 我看到了 this question ,但找不到有关使用 source: :query
写作的python工具,awscli-bastion , 具有以下由 cookiecutter 构建的目录结构. . ├── awscli_bastion │ ├── __init__.py │
Sphinx 文档生成器提供 only markup .例如,以下将仅包含外部文件“仅”如果其 html 生成器: .. only:: html .. include:: a.rst 但是我将如
我在我的Rails应用程序中实现了 sphinx 搜索。 我想模糊搜索。它应该搜索拼写错误,例如,如果输入搜索查询charact * a * ristics,则应该搜索charact * e * ri
Sphinx-autodoc 将字典、列表和元组扁平化 - 使长的几乎不可读。也并不总是需要 pretty-print 格式,因为一些嵌套的容器比分列更好地保持扁平化。有没有办法显示源代码中输入的可迭
我正在使用 Sphinx 为我的项目编写文档,并且发现下面给出的两个相似的 reStructuredText 段的呈现方式有所不同。 示例 1 Some text: * Item 0 * Item
考虑ReStructuredText中的以下列表: Broken list example ------------------- #. First do spam #. Then do ``eggs
我正在使用 Sphinx Doc 为我的一个项目创建文档,并且我在整个文档中多次使用了一些词,例如 - IP 地址、端口号和许多其他可能会随时间变化的内容。如果由于某种原因,其中一个将被更改,我只想在
我在 .rst 文件中有以下文本: Some text. * Heading | The first topic. | Another topic which is very verbose
我有很多 Sphinx 页面,它们都有相同的链接。像那些: .. _CC-BY: https://creativecommons.org/licenses/by/3.0/ .. _MIT: http:
我想链接到我的狮身人面像文档中的一些URL: blah 我在文档中发现了类似的内容:http://sphinx-doc.org/ext/extlinks.html-而是按照约定用链接替换自定义语法。
使用 sphinx 的自动模块 (https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html) 时, 我只是写在一个 .rst
我是一名优秀的程序员,十分优秀!