微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Sphinx 不从父类继承文档字符串

如何解决Sphinx 不从父类继承文档字符串

这里有很多关于 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 的上游项目。如果我能在本地解决这个问题会更好(更快)。

非常感谢任何关于正在发生的事情或如何前进的指示。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。