Sphinx Duplicate descriptions with autosummary_imported_members = True

In Sphinx-autosummary, I set the autosummary to display methods and classes with autosummary_imported_members = True, so I have a clear list of functions and methods. Further, I kept modules in the template as some submodules are imported and have members that I also need documented. However, now, functions and classes are documented twice, as such and as modules (module.submodule.function() or module.submodule.file that contains only the class) in the module documentation.

Is there any way to exclude the “modules” that are already in classes?

It is especially annoying when the module.submodule.file.class shouldn’t even be accessible as it is not in the __init__.py file.

I tried to add custom python functions in the template but it can never find them.

As an example:
My module template is:

{{ fullname | escape | underline}}


.. THIS USES THE MODULE TEMPLATE


.. automodule:: {{ fullname }}

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

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

   {% block functions %}
   {% if functions %}
   .. rubric:: {{ _('Functions') }}

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

   {% block classes %}
   {% if classes %}
   .. rubric:: {{ _('Classes') }}

   .. autosummary::
      :toctree:
      :recursive:
   {% for item in classes %}
      {{ item }}
   {%- endfor %}
   {% endif %}
   {% endblock %}

   {% block exceptions %}
   {% if exceptions %}
   .. rubric:: {{ _('Exceptions') }}

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





{% block modules %}
{% if modules %}
.. rubric:: Modules

.. autosummary::
   :toctree:
   :recursive:
{% for item in modules %}
   {{ item }}
{%- endfor %}
{% endif %}

If I have a module foo.bar with __init__.py:

from . import sub
from .tea import tea
from .data_class import DataClass

It will show as functions tea, as class DataClass, and as modules foo.bar.sub, foo.bar.tea, and foo.bar.data_class, when I would like it to only show foo.bar.sub.

Leave a Comment