Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Sphinx reST Python docstring field for yields?

Tags:

I'm trying to use reST-style docstrings, i.e.

def foo(bar):     """a method that takes a bar      :param bar: a Bar instance     :type bar: Bar 

Is there a standard way to document yields? I looked at http://www.sphinx-doc.org/en/master/usage/restructuredtext/domains.html#info-field-lists, a-la this question [ https://stackoverflow.com/questions/5334531/python-documentation-standard-for-docstring ], but no luck. I'm imagining something like,

:yields: transformed bars :yield type: Baz 
like image 761
gatoatigrado Avatar asked Jun 27 '14 22:06

gatoatigrado


People also ask

Does Sphinx work with Google docstrings?

Napoleon is a extension that enables Sphinx to parse both NumPy and Google style docstrings - the style recommended by Khan Academy.

How does Sphinx Autodoc work?

autodoc provides several directives that are versions of the usual py:module , py:class and so forth. On parsing time, they import the corresponding module and extract the docstring of the given objects, inserting them into the page source under a suitable py:module , py:class etc. directive.

How do you generate a docstring in Python?

Declaring Docstrings: The docstrings are declared using ”'triple single quotes”' or “””triple double quotes””” just below the class, method or function declaration. All functions should have a docstring.

Does Sphinx support markdown?

To support Markdown-based documentation, Sphinx can use MyST-Parser. MyST-Parser is a Docutils bridge to markdown-it-py, a Python package for parsing the CommonMark Markdown flavor.


2 Answers

Python 3.5 Iterator[] annotation

They offer a standardized Iterator[] syntax for this as documented at: https://docs.python.org/3/library/typing.html#typing.Generator

Before Python 3, I recommend that you use this syntax to make it easier to port later on:

from typing import List def f():     """     :rtype: Iterator[:class:`SomeClass`]     """     yield SomeClass() 

And after Python 3, use https://pypi.python.org/pypi/sphinx-autodoc-annotation with syntax:

from typing import Iterator def f() -> Iterator[SomeClass]:     yield SomeClass() 

I have reviewed the other answer and it doesn't in my opinion answer the question.

The way to document a generator, although not the best, is by using :return as in the rest of the docs. Use the description to give notice that it is a generator.

Yields from Google/Numpy style docs convert yields to return clauses.

https://bitbucket.org/RobRuana/sphinx-contrib/src/a06ae33f1c70322c271a97133169d96a5ce1a6c2/napoleon/sphinxcontrib/napoleon/docstring.py?at=default&fileviewer=file-view-default#docstring.py-678:680

like image 26
txomon Avatar answered Sep 18 '22 17:09

txomon