Skip to content

Commit

Permalink
Merge pull request #3468 from RelentlessIdiot/numpydoc_returns_unnamed
Browse files Browse the repository at this point in the history
ENH: Allow unnamed return values in Returns section of doc string
  • Loading branch information
rgommers committed Jun 29, 2013
2 parents 069e9b0 + 2241e6c commit 1d67e26
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 26 deletions.
22 changes: 19 additions & 3 deletions doc/HOWTO_DOCUMENT.rst.txt
Expand Up @@ -225,14 +225,30 @@ The sections of the docstring are:

5. **Returns**

Explanation of the returned values and their types, of the same
format as **parameters**.
Explanation of the returned values and their types. Similar to the
**parameters** section, except the name of each return value is optional.
The type of each return value is always required::

Returns
-------
int
Description of anonymous integer return value.

If both the name and type are specified, the **returns** section takes the
same form as the **parameters** section::

Returns
-------
err_code : int
Non-zero value indicates error code, or zero on success.
err_msg : str or None
Human readable error message, or None on success.

6. **Other parameters**

An optional section used to describe infrequently used parameters.
It should only be used if a function has a large number of keyword
prameters, to prevent cluttering the **parameters** section.
parameters, to prevent cluttering the **parameters** section.

7. **Raises**

Expand Down
12 changes: 5 additions & 7 deletions doc/example.py
Expand Up @@ -57,14 +57,12 @@ def foo(var1, var2, long_var_name='hi') :
Returns
-------
type
Explanation of anonymous return value of type ``type``.
describe : type
Explanation
output : type
Explanation
tuple : type
Explanation
items : type
even more explaining
Explanation of return value named `describe`.
out : type
Explanation of `out`.
Other Parameters
----------------
Expand Down
5 changes: 4 additions & 1 deletion doc/sphinxext/numpydoc/docscrape.py
Expand Up @@ -334,7 +334,10 @@ def _str_param_list(self, name):
if self[name]:
out += self._str_header(name)
for param,param_type,desc in self[name]:
out += ['%s : %s' % (param, param_type)]
if param_type:
out += ['%s : %s' % (param, param_type)]
else:
out += [param]
out += self._str_indent(desc)
out += ['']
return out
Expand Down
36 changes: 29 additions & 7 deletions doc/sphinxext/numpydoc/docscrape_sphinx.py
Expand Up @@ -42,16 +42,37 @@ def _str_summary(self):
def _str_extended_summary(self):
return self['Extended Summary'] + ['']

def _str_returns(self):
out = []
if self['Returns']:
out += self._str_field_list('Returns')
out += ['']
for param, param_type, desc in self['Returns']:
if param_type:
out += self._str_indent(['**%s** : %s' % (param.strip(),
param_type)])
else:
out += self._str_indent([param.strip()])
if desc:
out += ['']
out += self._str_indent(desc, 8)
out += ['']
return out

def _str_param_list(self, name):
out = []
if self[name]:
out += self._str_field_list(name)
out += ['']
for param,param_type,desc in self[name]:
out += self._str_indent(['**%s** : %s' % (param.strip(),
param_type)])
out += ['']
out += self._str_indent(desc,8)
for param, param_type, desc in self[name]:
if param_type:
out += self._str_indent(['**%s** : %s' % (param.strip(),
param_type)])
else:
out += self._str_indent(['**%s**' % param.strip()])
if desc:
out += ['']
out += self._str_indent(desc, 8)
out += ['']
return out

Expand Down Expand Up @@ -196,8 +217,9 @@ def __str__(self, indent=0, func_role="obj"):
out += self._str_index() + ['']
out += self._str_summary()
out += self._str_extended_summary()
for param_list in ('Parameters', 'Returns', 'Other Parameters',
'Raises', 'Warns'):
out += self._str_param_list('Parameters')
out += self._str_returns()
for param_list in ('Other Parameters', 'Raises', 'Warns'):
out += self._str_param_list(param_list)
out += self._str_warnings()
out += self._str_see_also(func_role)
Expand Down
33 changes: 25 additions & 8 deletions doc/sphinxext/numpydoc/tests/test_docscrape.py
Expand Up @@ -47,6 +47,9 @@
In other words, each entry ``out[i,j,...,:]`` is an N-dimensional
value drawn from the distribution.
list of str
This is not a real return value. It exists to test
anonymous return values.
Other Parameters
----------------
Expand Down Expand Up @@ -148,13 +151,19 @@ def test_other_parameters():
assert desc[0].startswith('A parrot off its mortal coil')

def test_returns():
assert_equal(len(doc['Returns']), 1)
assert_equal(len(doc['Returns']), 2)
arg, arg_type, desc = doc['Returns'][0]
assert_equal(arg, 'out')
assert_equal(arg_type, 'ndarray')
assert desc[0].startswith('The drawn samples')
assert desc[-1].endswith('distribution.')

arg, arg_type, desc = doc['Returns'][1]
assert_equal(arg, 'list of str')
assert_equal(arg_type, '')
assert desc[0].startswith('This is not a real')
assert desc[-1].endswith('anonymous return values.')

def test_notes():
assert doc['Notes'][0].startswith('Instead')
assert doc['Notes'][-1].endswith('definite.')
Expand Down Expand Up @@ -218,6 +227,9 @@ def test_str():
In other words, each entry ``out[i,j,...,:]`` is an N-dimensional
value drawn from the distribution.
list of str
This is not a real return value. It exists to test
anonymous return values.
Other Parameters
----------------
Expand All @@ -226,12 +238,12 @@ def test_str():
Raises
------
RuntimeError :
RuntimeError
Some error
Warns
-----
RuntimeWarning :
RuntimeWarning
Some warning
Warnings
Expand Down Expand Up @@ -334,6 +346,11 @@ def test_sphinx_str():
In other words, each entry ``out[i,j,...,:]`` is an N-dimensional
value drawn from the distribution.
list of str
This is not a real return value. It exists to test
anonymous return values.
:Other Parameters:
**spam** : parrot
Expand All @@ -342,13 +359,13 @@ def test_sphinx_str():
:Raises:
**RuntimeError** :
**RuntimeError**
Some error
:Warns:
**RuntimeWarning** :
**RuntimeWarning**
Some warning
Expand Down Expand Up @@ -698,11 +715,11 @@ def test_class_members_doc():
Methods
-------
a :
a
b :
b
c :
c
.. index::
Expand Down

0 comments on commit 1d67e26

Please sign in to comment.