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

时间:2017-05-15 15:48:40

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

我使用sphinx.ext.autosummary生成python文档。 autodoc和autosummary在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 %}

它工作正常,但这会添加文档中继承的每个方法,而标记显示继承' (它应该添加每个继承的成员)不存在。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

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

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

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