Sphinx 自动摘要:数据类继承

发布于 2025-01-20 14:24:06 字数 1502 浏览 1 评论 0 原文

以下代码使Sphinx AutoSummary失败:

@dataclasses.dataclass
class Foo:
    bar: int


class FooChild(Foo):
    pass

我的autosummary模板( _templates/autosummary/class.rst ):

{{ name | escape | underline}}

.. currentmodule:: {{ module }}

.. autoclass:: {{ objname }}
   :members:

   {% block attributes %}
   {% if attributes %}
   .. rubric:: {{ _('Attributes') }}

   .. autosummary::
   {% for item in attributes %}
      ~{{ name }}.{{ item }}
   {%- endfor %}
   {% endif %}
   {% endblock %}

运行 sphinx -build -j 2 -w -b html docs/gh docs/gh pogs docs/gh pages 我会遵循警告:

Warning, treated as error:
~/path/docs/generated/module.FooChild.rst:14:autosummary: 
failed to import FooChild.bar.

我需要将警告视为错误,因为否则我的项目中的文档会很快降级,但是我想修复此根本原因或忽略此特定的警告。

我找不到任何方法可以忽略源代码注释中的警告,也不能选择性地抑制狮身人面像配置中的警告。请帮忙!

编辑: 这是生成的 foochild.rst 文件看起来像:

FooChild
========
.. currentmodule:: package.module

.. autoclass:: FooChild
   :members:

   .. rubric:: Attributes
   .. autosummary::
      ~FooChild.bar

我有一个 conf.py api.rst 添加到toctree, api。 RST 旨在触发自室以创建所有模块和类创建文档,在此示例中,只有 module.py.py

API Overview
************

.. currentmodule:: package
.. autosummary::
   :toctree: generated
   :recursive:

   module

Following code makes Sphinx autosummary fail:

@dataclasses.dataclass
class Foo:
    bar: int


class FooChild(Foo):
    pass

My autosummary template (_templates/autosummary/class.rst):

{{ name | escape | underline}}

.. currentmodule:: {{ module }}

.. autoclass:: {{ objname }}
   :members:

   {% block attributes %}
   {% if attributes %}
   .. rubric:: {{ _('Attributes') }}

   .. autosummary::
   {% for item in attributes %}
      ~{{ name }}.{{ item }}
   {%- endfor %}
   {% endif %}
   {% endblock %}

When running sphinx-build -j 2 -W -b html docs docs/gh-pages I get following warning:

Warning, treated as error:
~/path/docs/generated/module.FooChild.rst:14:autosummary: 
failed to import FooChild.bar.

I need to treat warnings as errors because otherwise documentation in my project will quickly degrade, but I would like to either fix root cause of this one or ignore this specific warning.

I couldn't find any way to ignore a warning from source code comments, nor to selectively suppress warnings from Sphinx configuration. Please help!

EDIT:
here is what generated FooChild.rst file looks like:

FooChild
========
.. currentmodule:: package.module

.. autoclass:: FooChild
   :members:

   .. rubric:: Attributes
   .. autosummary::
      ~FooChild.bar

I have a conf.py adding api.rst to toctree, api.rst is designed to trigger autosummary to create documentation for all modules and classes, in this example just module.py.

API Overview
************

.. currentmodule:: package
.. autosummary::
   :toctree: generated
   :recursive:

   module

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

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

发布评论

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

评论(1

温柔女人霸气范 2025-01-27 14:24:06

虽然它没有修复根本原因,但是您要求一种忽略此警告的方法:

您可以在模块中添加 __所有__ ,但不包括 foochild 。这样,AutoSummary将忽略它。

来自文档 autosummary_ignore_module_all

如果false和一个模块具有 __所有__ 属性集,则在 __ ALL __ ALL __ 中列出的每个成员,也没有其他成员。默认值为true

请注意,如果在 __ All __ 中列出导入的成员,则无论AutoSummary_imported_members的价值如何,都将记录在记录中。要匹配来自模块导入 *的行为 *,请将autosummary_ignore_module_all设置为false和autosummary_imported_members true。

版本4.4中的新事物。

While it doesn't fix the root cause, but you asked for a way to ignore this warning:

You could add __all__ to your module, that doesn't include FooChild. This way it will be ignored by autosummary.

From documentation autosummary_ignore_module_all https://www.sphinx-doc.org/en/master/usage/extensions/autosummary.html:

If False and a module has the __all__ attribute set, autosummary documents every member listed in __all__ and no others. Default is True

Note that if an imported member is listed in __all__, it will be documented regardless of the value of autosummary_imported_members. To match the behaviour of from module import *, set autosummary_ignore_module_all to False and autosummary_imported_members to True.

New in version 4.4.

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