Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I document lists, optionals, and yields using Google-style Sphinx? [closed]

How do I indicate types for lists, optional arguments and return types for generators on Google-style docstrings using Sphinx-Napoleon?

I've tried

List[type] list of type  Optional[type] type, optional 

and

Yields:    type:  

respectively; but all produce unsatisfactory output that is not consistent with the rest of the generated documentation. For example

Optional[type] 

just gives

Optional[type]

without any link for type.

I've tried every builtin theme and have the same issues.

How should I be documenting these elements using Google-style docstrings with Sphinx-Napoleon?

like image 671
orome Avatar asked Dec 04 '15 23:12

orome


People also ask

How do you write a docstring?

The doc string line should begin with a capital letter and end with a period. The first line should be a short description. If there are more lines in the documentation string, the second line should be blank, visually separating the summary from the rest of the description.

What are Docstrings in Python how are they useful?

As mentioned above, Python docstrings are strings used right after the definition of a function, method, class, or module (like in Example 1). They are used to document our code. We can access these docstrings using the __doc__ attribute.

Which is the correct location to place a docstring for a function?

A docstring is a string literal placed in the first line of a function body, as shown below.

How do you print a docstring in a function?

Try: class MyClass(): # ... def my_function(self): """Docstring for my function""" print MyClass. my_function.


1 Answers

I know this is quite old, but did you take a look at this example? Particularly lines:

def __init__(self, param1, param2, param3):         """Example of docstring on the __init__ method.          The __init__ method may be documented in either the class level         docstring, or as a docstring on the __init__ method itself.          Either form is acceptable, but the two should not be mixed. Choose one         convention to document the __init__ method and be consistent with it.          Note:             Do not include the `self` parameter in the ``Args`` section.          Args:             param1 (str): Description of `param1`.             param2 (:obj:`int`, optional): Description of `param2`. Multiple                 lines are supported.             param3 (:obj:`list` of :obj:`str`): Description of `param3`.          """         self.attr1 = param1         self.attr2 = param2         self.attr3 = param3  #: Doc comment *inline* with attribute          #: list of str: Doc comment *before* attribute, with type specified         self.attr4 = ['attr4']          self.attr5 = None         """str: Docstring *after* attribute, with type specified.""" 
like image 112
Colonder Avatar answered Sep 23 '22 14:09

Colonder