11

I am using NumPyDoc-style docstrings to document a Python package. I would like to switch from the 'numpydoc' Sphinx extension to Napoleon, because I find that it formats the docstring in a more compact and readable way. However, it does not list the methods of a class at the top of the documentation, which I find a very valuable feature of numpydoc. Does anyone know how to switch this on manually in Napoleon?

barryhunter
  • 20,886
  • 3
  • 30
  • 43
Pietro Marchesi
  • 852
  • 2
  • 8
  • 18

2 Answers2

0

sphinx.ext.autosummary helps to achieve a Sphinx table of contents for the lists of class methods and attributes

I went through both the numpydoc and Sphinx documentation to understand how it works. Do find the details below.

SECTION 1 : understanding working of numpydoc

Numpydoc documentation provides an explanation how the table of all class methods is implemented by default. It internally uses sphinx.ext.autosummary and numpydoc_class_members_toctree

  • when numpydoc is added to extensions option in conf.py, sphinx.ext.autosummary will automatically be loaded as well.

  • The following configurations options are True by default in numpydoc

    numpydoc_show_class_members = True #shows all members of a class in the Methods and Attributes 
    numpydoc_show_inherited_class_members = True #shows all inherited members of a class 
    numpydoc_class_members_toctree = True #creates a Sphinx table of contents for the lists of class methods and attributes. 
    

SECTION 2 : implementation with Sphinx extensions

Sphinx extensions : To implement table of all class methods, use autosummary along with autodoc extensions

Napolean documentation quotes

Napoleon interprets every docstring that Sphinx autodoc can find, including docstrings on: modules, classes, attributes, methods, functions, and variables

That is sphinx.ext.napoleon works along sphinx.ext.autodoc

  1. First step is to explicitly include both sphinx.ext.autodoc and sphinx.ext.autosummary when you include sphinx.ext.napoleon in extensions option in your Sphinx conf.py

    extensions = [
      'sphinx.ext.autodoc',  # Core Sphinx library for auto html doc generation from docstrings
      'sphinx.ext.autosummary',  # Create neat summary tables for modules/classes/methods etc    
      'sphinx.ext.napoleon', # Support for NumPy and Google style docstrings  
    ] 
    
  2. Second steps is to select directive and relevant options, autodoc documentation provides 3 directives automodule, autoclass and autoexception to document a module, class or exception respectively

    .. autoclass:: Noodle       
       :members:    #option to generate document for the members of the target module, class or exception
       :undoc-members:      #option to generate document for the members not having docstrings
       :inherited-members:  #option to include members inherited from base classes
       :private-members:    #option to generate document for the private members
       :special-members:  #option to generate document for the special members (like __special__)
    

    If you want set the above options as default in autodoc, you can set them in autodoc_default_options as mentioned in this documentation

       autodoc_default_options = {
           'members': True,
           'imported-members': True,
           'undoc-members': True,
       }
    

    Other supported options are 'members', 'member-order', 'undoc-members', 'private-members', 'special-members', 'inherited-members', 'show-inheritance', 'ignore-module-all', 'imported-members', 'exclude-members', 'class-doc-from','imported-members', 'class-doc-from'

  3. Third and final step to set relevant options, autosummary documentation generates autodoc summaries i.e., function/method/attribute summary lists

    .. autosummary::
       :toctree: DIRNAME              #example from documentation
    
       sphinx.environment.BuildEnvironment
       sphinx.util.relative_uri
    

    The autosummary directive can also optionally serve as a toctree entry for the included items. Optionally, stub .rst files for these items can also be automatically generated when autosummary_generate is True.

    The following configuration options should also be set in your Sphinx conf.py:

    # generate autosummary even if no references
    autosummary_generate = True
    autosummary_imported_members = True
    
Archana David
  • 1,354
  • 2
  • 15
  • 26
  • This seems very close, but when I tried this on a class, the table created by autosummary contained only a single line item for the class itself - it did not include members. Is this your experience? If not, I must be missing something. – SethMMorton Nov 26 '21 at 17:07
  • autosummary’s options must be set to **True** namely `autosummary_generate` and `autosummary_imported_members`. Note that autosummary is also dependent on `autodoc` extension so you must include autoclass directive as mentioned in step 2. I have also included 3 complete steps to follow – Archana David Nov 26 '21 at 17:18
  • Ah - I *was* missing something. It creates an rst file in the DIRNAME and *that* is where the new TOC is found. – SethMMorton Nov 26 '21 at 17:28
  • In step 2, I have also included a way to set the default behaviour of autodoc `autodoc_default_options`, depending on your requirements you can add the required supported options like `inherited-members` mentioned below. Do follow the 3 important steps – Archana David Nov 26 '21 at 17:31
-1

I am new to Napoleon/Sphinx, but I think the answer might be in Sphinx, rather than Napoleon.

If you have autodoc enabled in conf.py, eg.

extensions = ['sphinx.ext.autodoc',
    'sphinx.ext.napoleon',
    'sphinx.ext.coverage']

then add to your index.rst (say):

.. autoclass:: module_name.class_name
    :members:
    :undoc-members:

where module_name contains the class in question, then class_name will be in the documentation along with all of its methods, even if they do not currently have a docstring.

Will Bryant
  • 521
  • 5
  • 17
  • The issue is not that methods are not showing at all, but rather there is no hyperlinked list of methods at the top (like a table of contents). – SethMMorton Sep 16 '19 at 17:09
  • @SethMMorton might I just clarify your question, so you are looking to switch from numpydoc to sphinx ext napoleon. And Your question is more related to table of contents – Archana David Nov 25 '21 at 18:49
  • The official documentation for sphinx(napoleon) in the question latest software update was in 2016 apart from this, are you open to any new solutions as well? – Archana David Nov 25 '21 at 18:52
  • @ArchanaDavid The question is this: with the numpydoc extension a table of all class methods created in class documentation which greatly aids discoverability. Napoleon does not do this, at least by default, but it is the assumption of the OP and myself that there must be a way to enable it and we would like to know how. – SethMMorton Nov 25 '21 at 18:53
  • @ArchanaDavid And yes, any viable solution is fine. – SethMMorton Nov 25 '21 at 23:03