显示Sphinx Autodoc中继承属性的DocString
我有两个类,一个基类和一个从此基类继承的类
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:我在这里给出的是一个玩具示例,我记录的项目更大,并且不适合问题。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在上面的示例中,实例化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).