python - 如何避免继承成员使用自动摘要和自定义模板?

标签 python python-3.x python-sphinx autodoc

我使用 sphinx.ext.autosummary 生成一个 python 文档。 autodocautosummary 在 conf.py 中配置如下:

autodoc_member_order = 'bysource'
## Default flags used by autodoc directives
autodoc_default_flags = ['members','undoc-members']
## Generate autodoc stubs with summaries from code
autosummary_generate = True

我使用模板:

myModuleName
=======

.. autosummary::
   :toctree: _autosummary
   :template: modules.rst

   myModule

模块模板是:

{{ fullname }}
{{ underline }}

.. automodule:: {{ fullname }}

   {% block functions %}
   {% if functions %}
   .. rubric:: Functions

   .. autosummary::
      :toctree: {{ objname }}
   {% for item in functions %}
      {{ item }}
   {%- endfor %}
   {% endif %}
   {% endblock %}

   {% block classes %}
   {% if classes %}
   .. rubric:: Classes

   .. autosummary::
      :toctree: {{ objname }}
      :template: class.rst
   {% for item in classes %}
      {{ item }}
   {%- endfor %}
   {% endif %}
   {% endblock %}

   {% block exceptions %}
   {% if exceptions %}
   .. rubric:: Exceptions

   .. autosummary::
   {% for item in exceptions %}
      {{ item }}
   {%- endfor %}
   {% endif %}
   {% endblock %}

类模板是:

{{ fullname }}
{{ underline }}

.. currentmodule:: {{ module }}

.. autoclass:: {{ objname }}

   {% block methods %}

   {% if methods %}
   .. rubric:: Methods

   .. autosummary::
      :toctree: {{ objname }}
   {% for item in methods %}
      ~{{ name }}.{{ item }}
   {%- endfor %}
   {% endif %}
   {% endblock %}

   {% block attributes %}
   {% if attributes %}
   .. rubric:: Attributes

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

它工作正常,但是这会添加文档中继承的每个方法,而标志“show-inheritance”(应该添加每个继承的成员)不存在。

有什么想法吗?

最佳答案

似乎确实没有任何标志(例如 :no-inherited-members:)对此有任何影响,但您可以修改您的类模板来解决问题。

{% for item in methods %}
{%- if item not in inherited_members %}
    ~{{ name }}.{{ item }}
{%- endif %}
{%- endfor %}

以上似乎对我有用。 希望对你有帮助...

关于python - 如何避免继承成员使用自动摘要和自定义模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43983799/

相关文章:

python - 无法使用 python-nvd3

python - django 在保留数据的同时将 OneToOneField 移动到外键

python - 如何在 Sphinx 处理的文档字符串中表示单个参数或返回值的多种类型?

python - 如何在python中用该字符的单个实例替换字符的重复实例

python nonlocal - 为什么有时需要它有时不需要

python - 如何在python中定义归因的归属

python - Related Manager 一个方向为空,另一个方向为空

python - 列表理解Python素数

python - 覆盖 sphinx/docutils 中的默认字段名称限制

markdown - 摆脱 Sphinx 中的 "duplicate label"警告