5

Is it possible to add docstrings for groups of methods in the Sphinx generated documentation?

For example, I would like to have something like:

class MyClass():
    """Doc of the class"""
    def __init__(self):
        pass

    """----- The following part is about imports -----"""

    def import_from_source_1(self):
        """Doc of import_from_source_1"""
        pass

    def import_from_source_2(self):
        """Doc of import_from_source_2"""
        pass

    """----- The following part is about exports-----"""

    def export_to_dest_1(self):
        """Doc of export_to_dest_1"""
        pass

    def export_to_dest_2(self):
        """Doc of export_to_dest_2"""
        pass

And the expected output would be:

MyClass
    Doc of the class

----- The following part is about imports -----
import_from_source_1
    Doc of import_from_source_1

import_from_source_2
    Doc of import_from_source_2

----- The following part is about exports-----
export_to_dest_1
    Doc of export_to_dest_1

export_to_dest_2
    Doc of export_to_dest_2

Note that my goal is not (only) to group methods (as found in Group method docstrings in sphinx), but to add a docstring to the group.

mzjn
  • 48,958
  • 13
  • 128
  • 248
Simpom
  • 938
  • 1
  • 6
  • 23
  • Could anyone explain the downvote so that I can improve my question? – Simpom Feb 07 '18 at 22:20
  • 1
    A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. See https://www.python.org/dev/peps/pep-0257/#id15. You cannot have "extra" docstrings as in your example. – mzjn Feb 08 '18 at 07:42
  • I see. So there isn't any solution to group methods? – Simpom Feb 08 '18 at 09:03

1 Answers1

8

A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition (https://www.python.org/dev/peps/pep-0257/#what-is-a-docstring). You cannot have "extra" docstrings like the ones in the question.

However, you can do the grouping by using automethod:

.. currentmodule:: mymodule

.. autoclass:: MyClass
   
   The following part is about imports
 
   .. automethod:: import_from_source_1
   .. automethod:: import_from_source_2
 
   The following part is about exports
 
   .. automethod:: export_to_dest_1
   .. automethod:: export_to_dest_2
mzjn
  • 48,958
  • 13
  • 128
  • 248
  • 1
    This solution is a good compromise for me: as I need to organize the mothods by myself, their documentation cannot be fully automatic (sounds obvious...). – Simpom Feb 08 '18 at 10:08