Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Sphinx napoleon document function returning multiple arguments?

I am trying to use the Google code style to document a function that I then use sphinx with the napoleon extension to create documentation for. The function is unusual in that is returns two arguments. I don't think napoleon handles this. If so, could someone tell me how they handle it?

def foo(a): '''one line summary  longer explanation  Args:   a (int): parameter description  Returns:   servers (list): list of servers to use   msg (str): logging message string  ''' pass 

Maybe I'm getting a message that it is not great coding style to return multiple arguments, but can you do this? The html generated treats those two lines as part of a description for one argument. If I put a newline between the servers and msg line, it helps, but it is still documenting one arg.

like image 808
MrCartoonology Avatar asked Mar 23 '15 22:03

MrCartoonology


People also ask

What is Sphinx Napoleon?

Napoleon is a extension that enables Sphinx to parse both NumPy and Google style docstrings - the style recommended by Khan Academy. Napoleon is a pre-processor that parses NumPy and Google style docstrings and converts them to reStructuredText before Sphinx attempts to parse them.

Should all functions have a Docstring?

All functions should have a docstring. Accessing Docstrings: The docstrings can be accessed using the __doc__ method of the object or using the help function. The below examples demonstrates how to declare and access a docstring.

When defining a Docstring you should use?

Docstrings are defined with triple-double quote(“””) string format. At a minimum, a Python docstring should give a quick summary of whatever the function is doing. A function's docstring can be accessed in two ways.

What is a Docstring Why are they written?

A Python docstring is a string used to document a Python module, class, function or method, so programmers can understand what it does without having to read the details of the implementation. Also, it is a common practice to generate online (html) documentation automatically from docstrings.


2 Answers

Python only returns a single object. If you call

serv,msg = foo(myinput) 

Then you are explicitly expanding the expression_list tuple which is generated when the function returns with this code

return servers,msg 

You docstring should read some thing like this (with the Napoleon Google Style)

""" one line summary  longer explanation  Args:     a (int): parameter description  Returns:     (tuple): tuple containing:          servers(list) servers to use         msg (str): logging message string  """ 

Or with the Napoleon NumPy style:

""" one line summary  longer explanation  Parameters ---------- a : int     parameter description  Returns ------- servers : list     servers to use msg : str     logging message string  """ 

Have a look at the python docs for return and perhaps expression_list

like image 79
mor22 Avatar answered Oct 09 '22 06:10

mor22


Google style does not support multiple return values. As a workaround you can use:

Returns:     2-element tuple containing      - **rates** (*array*): the unnormalized rates (just the sum of the       exponential kernels). To obtain rates in Hz divide the       array by `2*tau` (or other conventional `x*tau` duration).     - **nph** (*array*): number of photons in -5*tau..5*tau window       for each timestamp. Proportional to the rate computed       with KDE and rectangular kernel. 

This results in a nice output even with multi-line description for each returned item.

like image 29
user2304916 Avatar answered Oct 09 '22 04:10

user2304916