Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does NumPy process docstrings into sphinx documentation for Parameters?

I want to build our documentation using sphinx and get the same formatting on parameters as the NumPy docs ( https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt )

I have found two ways to document parameters in this rst style with sphinx, one which is

:param name: description

or

:keyword name: description

and the other (which is the NumPy style)

Parameters
----------
name: type
    description

Here is an example of what that looks like:

http://docs.scipy.org/doc/numpy/reference/distutils.html#module-numpy.distutils

and the source

def get_subpackage(self,subpackage_name,
                   subpackage_path=None,
                   parent_name=None,
                   caller_level = 1):
    """Return list of subpackage configurations.

    Parameters
    ----------
    subpackage_name: str,None
        Name of the subpackage to get the configuration. '*' in
        subpackage_name is handled as a wildcard.
    subpackage_path: str
        If None, then the path is assumed to be the local path plus the
        subpackage_name. If a setup.py file is not found in the
        subpackage_path, then a default configuration is used.
    parent_name: str
        Parent name.
    """

However, when I build the docs with sphinx ( I am using sphinx-apidoc and sphinx-build ), I can generate the formatted lists when I use the first syntax ( :param name: description), but when I try to use the NumPy style I do not get the formatting. Looking at the rst syntax ( http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#sections ) it seems that something like

Parameters
----------

is just a section title. But using this formatting with sphinx the title Parameter does not appear in the output, and it gets none of the parameter section formatting.

Does anyone know how NumPy builds the documentation with sphinx to get this sort of formatting to work for parameters?

I have tried to look at the makefile and conf.py, and I'm just not sure how

like image 303
jkeesh Avatar asked Jul 01 '13 04:07

jkeesh


People also ask

How do you format a Sphinx Docstring?

The Sphinx docstring formatA pair of :param: and :type: directive options must be used for each parameter we wish to document. The :raises: option is used to describe any errors that are raised by the code, while the :return: and :rtype: options are used to describe any values returned by our code.

What are NumPy docstrings?

numpydoc style docstrings are written in restructured text. They are composed of a short description of the object followed by a few required and optional sections. For functions and methods, these sections are: Required. Parameters.

How do you write a NumPy style Docstring?

The docstring in NumPy style consists of several sections. At a minimum, start with a short (one-line) summary of what the function does. If useful, add a few extra lines of extended summary. Then add a section describing the parameters and their types, and another section for the return values.

What does Sphinx Autodoc do?

autodoc imports the modules to be documented. If any modules have side effects on import, these will be executed by autodoc when sphinx-build is run. If you document scripts (as opposed to library modules), make sure their main routine is protected by a if __name__ == '__main__' condition.


1 Answers

NumPy uses a custom Sphinx extension: https://pypi.python.org/pypi/numpydoc.

You can install it with

pip install numpydoc

and then you add it to the sphinx conf.py file by adding to the extensions list

extensions = ['sphinx.ext.autodoc', 'sphinx.ext.coverage', 'numpydoc']
like image 82
jkeesh Avatar answered Sep 22 '22 20:09

jkeesh