显示Sphinx Autodoc中继承属性的DocString

发布于 2025-01-27 09:50:41 字数 1554 浏览 0 评论 0 原文

我有两个类,一个基类和一个从此基类继承的类

from abc import ABC

class Base(ABC):
    """
    Base class
    
    Parameters
    ----------
    param1 : int


    Attributes
    ----------
    name : string
        identifier (not necessarily unique).
   
    """
    def __init__(self):
        self.name = "dummy_name"


class MyClass(Base):
    """
    Some other class 
    
    Parameters
    ----------
    param2 : int
   
    """

    def __init__(self):
        pass

,我想将基类的属性继承到使用AutoDocs的myclass中。但这似乎不起作用。

我的autodoc配置就是这样:

extensions = [
    "sphinx.ext.doctest",
    "sphinx.ext.todo",
    "sphinx.ext.viewcode",
    "sphinx.ext.autodoc",
    "sphinx.ext.autosummary",
    "sphinx.ext.mathjax",
    "sphinx.ext.autosectionlabel",
    "sphinxcontrib.video",
    "numpydoc",
    "sphinx_gallery.gen_gallery",
    "myst_parser",
]


autodoc_default_options = {"members": True, "inherited-members": True, "show-inheritance":True}
# generate autosummary even if no references
autosummary_generate = True
autodoc_inherit_docstrings = True

类模板是这样的:

:mod:`{{module}}`.{{objname}}
{{ underline }}==============

.. currentmodule:: {{ module }}

.. autoclass:: {{ objname }}

       
.. include:: {{module}}.{{objname}}.examples

.. raw:: html

    <div class="clearer"></div>

我尝试将:senasenited-Members:添加到类模板中,但没有更改任何内容。

PS:我在这里给出的是一个玩具示例,我记录的项目更大,并且不适合问题。

I have two classes, a base class and a class that inherits from this base class

from abc import ABC

class Base(ABC):
    """
    Base class
    
    Parameters
    ----------
    param1 : int


    Attributes
    ----------
    name : string
        identifier (not necessarily unique).
   
    """
    def __init__(self):
        self.name = "dummy_name"


class MyClass(Base):
    """
    Some other class 
    
    Parameters
    ----------
    param2 : int
   
    """

    def __init__(self):
        pass

and I want to inherit the attributes of the Base class into MyClass with autodocs. But it does not seem to work.

My autodoc config is like that :

extensions = [
    "sphinx.ext.doctest",
    "sphinx.ext.todo",
    "sphinx.ext.viewcode",
    "sphinx.ext.autodoc",
    "sphinx.ext.autosummary",
    "sphinx.ext.mathjax",
    "sphinx.ext.autosectionlabel",
    "sphinxcontrib.video",
    "numpydoc",
    "sphinx_gallery.gen_gallery",
    "myst_parser",
]


autodoc_default_options = {"members": True, "inherited-members": True, "show-inheritance":True}
# generate autosummary even if no references
autosummary_generate = True
autodoc_inherit_docstrings = True

and the class template is like that :

:mod:`{{module}}`.{{objname}}
{{ underline }}==============

.. currentmodule:: {{ module }}

.. autoclass:: {{ objname }}

       
.. include:: {{module}}.{{objname}}.examples

.. raw:: html

    <div class="clearer"></div>

I tried to add :inherited-members: to the class template but it did not change anything.

PS: what I give here is a toy example, the project I document is much larger and would not fit into a question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不气馁 2025-02-03 09:50:41

在上面的示例中,实例化myClass对象不能具有属性myClass.name。为此,您必须首先调用 super().__ INT __()

如果您使用属性名称作为类属性,则应使用。也许需要对类模板进行一些调整。类属性将是
sashited_members属性()。

In the above example, an instantiated MyClass object cannot have the attribute MyClass.name. For that you have to call super().__init__() first.

If you use the attribute name as a class attribute it should work. Maybe some tweaking of the class template is necessary. The class attribute will be part of the
inherited_members attribute in the template (https://www.sphinx-doc.org/en/master/usage/extensions/autosummary.html#inherited_members).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文