1

这里有很多关于 SO 的问题,询问在使用 sphinx 构建文档时如何避免继承父文档的文档字符串。我有相反的问题:我希望我的类级文档字符串被继承,但这没有发生。

我构建了一个玩具示例来说明问题。两个相关的类是@dataclasses

# parent.py

from dataclasses import dataclass


@dataclass
class Foo:
    """ A numpy conform docstring.
    
    This is the long description

    Attributes
    ----------
    a_number : int
        Just a boring number
    a_string : str
        A cool string.

    """
    a_number : int
    a_string : str
# child.py

from dataclasses import dataclass

from .parent import Foo


@dataclass
class Bar(Foo):
    a_second_string : str

在这些sphinx-quickstart文件之上,我修改了index.rst类的启动生成,并修改了配置以包括 autodoc、autosummary 和 numpydoc(也尝试了 napoleon 或 None,但都不在子节点上生成文档字符串)。

.. index.rst

FooBar
======

.. autosummary::
   :toctree: _autosummary

   foobar.parent.Foo
   foobar.child.Bar

# config.py

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys
sys.path.insert(0, os.path.abspath('..'))


# -- Project information -----------------------------------------------------

project = 'FooBar'
copyright = '2021, FirefoxMetzger'
author = 'FirefoxMetzger'


# -- General configuration ---------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.autosummary',
    # 'numpydoc'
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
#
html_theme = 'alabaster'

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']

作为参考,我在 Sphinx 4.1.2 上。


我可以(理论上)将该行添加__doc__ = Foo.__doc__Bar,这将手动导入文档字符串;但是在我的情况下 Foo 和 Bar 是自动生成的(XML 绑定)。我将不得不修改生成脚本,这只是部分可能,因为它是一个必须接受相应 PR 的上游项目。如果我能在本地解决这个问题会更好(更快)。

任何有关正在发生的事情或如何前进的指示都将受到高度赞赏。

4

0 回答 0