【问题标题】:Custom module name is not displayed with autoclass自定义模块名称不与 autoclass 一起显示
【发布时间】:2021-08-15 22:36:43
【问题描述】:

我正在使用 sphinx 构建文档,并且我的 .rst 文件之一具有以下内容:

############
Internal API
############

.. autoclass:: Individual::Individual
    :noindex:
    :special-members:
    :exclude-members: __weakref__
    :member-order: bysource
    :members:

.. autoclass:: exceptions::Error
    :noindex:
    :special-members:
    :exclude-members: __weakref__
    :member-order: bysource
    :members:

.. autoclass:: exceptions::IncorrectValueError
    :noindex:
    :special-members:
    :exclude-members: __weakref__
    :member-order: bysource
    :members:

构建成功,但请看附件截图:

问题在于Individual 类正确地以Individual 模块名称为前缀,而位于exceptions.py 中的后两个类却没有。为什么会发生这种情况,我应该如何解决?

编辑:我在下面添加conf.py 的内容(没有不必要的部分):

import os
import sys
import sphinx_rtd_theme
sys.path.insert(0, os.path.abspath('../moranpycess'))
extensions = [
    'sphinx.ext.napoleon',
    'sphinx_rtd_theme',
    'recommonmark',
]
templates_path = ['_templates']
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
html_theme = 'sphinx_rtd_theme'
html_show_sourcelink = False
html_static_path = ['_static']

还有exceptions.py的内容

class Error(Exception):
    """Base class for other exceptions.

    Args:
        Exception (Exception): built-in Exception class
    """

    pass

class IncorrectValueError(Error):
    """Handling incorrect values of user's arguments.

    Args:
        Error (Error): Base class for other exceptions.
    """

    def __init__(
        self,
        parameter,
        message="Please check the documentation for expected argument values.",
    ):
        """Class initializer.

        Args:
            parameter (str): parameter name
            message (str, optional): error message.
                Defaults to "Please check the documentation
                for expected argument values.".
        """
        self.parameter = parameter
        self.message = message
        super().__init__(self.message)

    def __str__(self):
        """Display the error message.

        Returns:
            str: error message
        """
        return f"Incorrect value for {self.parameter}. {self.message}"

【问题讨论】:

  • 您的自动文档指令中有几个错误。
  • 在不包含 Python 代码的情况下试图解释问题所在是没有任何意义的。请编辑问题以添加"Minimal, Reproducible, Example."。基本上,您要求我们通过查看 HTML 输出来推断您的 Python 代码。 .rst 中有太多错误需要解决,还必须推断 Python 源代码。
  • @bad_coder :我已经包含了 sphinx 的代码和配置。 .rst 文件中有那么多错误是什么?
  • 我可以重现这个。 exceptions.py 这个名字在某种程度上似乎是“特别的”。如果我将其更改为 exceptions1.py,则类名在输出中以 exceptions1. 为前缀。
  • 非常感谢@mzjn,我确认这个“解决方案”也适用于我......

标签: documentation python-sphinx python-module python-packaging


【解决方案1】:

这确实是sphinx中保留名称的一个特例:
https://github.com/sphinx-doc/sphinx/issues/9280

应该在新版本中修复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多