Skip to content

Commit 553e3ce

Browse files
ENH: Allow unnamed return values in Returns section of doc string
Developers usually only need the type of a return value followed by a brief description. However, in some cases providing a name for a return value can make the documentation clearer. This enhancement changes the format of the Returns section such that the type is required, and the name is optional: Returns ------- int Description of anonymous integer return value. x : str Description of string return value named `x`. With this change, if a colon is not present, then the entire line is interpreted as the return type. In all other cases, the Returns section is interpreted according to the current rules. Consistent with the current format, if a colon is present, then the text to the left of the colon is interpreted as the name; and the text to the right of the colon is interpreted as the type. This makes the proposed change backwards compatible with existing documentation.
1 parent 3070deb commit 553e3ce

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
lines changed

numpydoc/docscrape.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,10 @@ def _str_param_list(self, name):
334334
if self[name]:
335335
out += self._str_header(name)
336336
for param,param_type,desc in self[name]:
337-
out += ['%s : %s' % (param, param_type)]
337+
if param_type:
338+
out += ['%s : %s' % (param, param_type)]
339+
else:
340+
out += [param]
338341
out += self._str_indent(desc)
339342
out += ['']
340343
return out

numpydoc/docscrape_sphinx.py

+29-7
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,37 @@ def _str_summary(self):
4242
def _str_extended_summary(self):
4343
return self['Extended Summary'] + ['']
4444

45+
def _str_returns(self):
46+
out = []
47+
if self['Returns']:
48+
out += self._str_field_list('Returns')
49+
out += ['']
50+
for param, param_type, desc in self['Returns']:
51+
if param_type:
52+
out += self._str_indent(['**%s** : %s' % (param.strip(),
53+
param_type)])
54+
else:
55+
out += self._str_indent([param.strip()])
56+
if desc:
57+
out += ['']
58+
out += self._str_indent(desc, 8)
59+
out += ['']
60+
return out
61+
4562
def _str_param_list(self, name):
4663
out = []
4764
if self[name]:
4865
out += self._str_field_list(name)
4966
out += ['']
50-
for param,param_type,desc in self[name]:
51-
out += self._str_indent(['**%s** : %s' % (param.strip(),
52-
param_type)])
53-
out += ['']
54-
out += self._str_indent(desc,8)
67+
for param, param_type, desc in self[name]:
68+
if param_type:
69+
out += self._str_indent(['**%s** : %s' % (param.strip(),
70+
param_type)])
71+
else:
72+
out += self._str_indent(['**%s**' % param.strip()])
73+
if desc:
74+
out += ['']
75+
out += self._str_indent(desc, 8)
5576
out += ['']
5677
return out
5778

@@ -196,8 +217,9 @@ def __str__(self, indent=0, func_role="obj"):
196217
out += self._str_index() + ['']
197218
out += self._str_summary()
198219
out += self._str_extended_summary()
199-
for param_list in ('Parameters', 'Returns', 'Other Parameters',
200-
'Raises', 'Warns'):
220+
out += self._str_param_list('Parameters')
221+
out += self._str_returns()
222+
for param_list in ('Other Parameters', 'Raises', 'Warns'):
201223
out += self._str_param_list(param_list)
202224
out += self._str_warnings()
203225
out += self._str_see_also(func_role)

numpydoc/tests/test_docscrape.py

+25-8
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
4848
In other words, each entry ``out[i,j,...,:]`` is an N-dimensional
4949
value drawn from the distribution.
50+
list of str
51+
This is not a real return value. It exists to test
52+
anonymous return values.
5053
5154
Other Parameters
5255
----------------
@@ -148,13 +151,19 @@ def test_other_parameters():
148151
assert desc[0].startswith('A parrot off its mortal coil')
149152

150153
def test_returns():
151-
assert_equal(len(doc['Returns']), 1)
154+
assert_equal(len(doc['Returns']), 2)
152155
arg, arg_type, desc = doc['Returns'][0]
153156
assert_equal(arg, 'out')
154157
assert_equal(arg_type, 'ndarray')
155158
assert desc[0].startswith('The drawn samples')
156159
assert desc[-1].endswith('distribution.')
157160

161+
arg, arg_type, desc = doc['Returns'][1]
162+
assert_equal(arg, 'list of str')
163+
assert_equal(arg_type, '')
164+
assert desc[0].startswith('This is not a real')
165+
assert desc[-1].endswith('anonymous return values.')
166+
158167
def test_notes():
159168
assert doc['Notes'][0].startswith('Instead')
160169
assert doc['Notes'][-1].endswith('definite.')
@@ -218,6 +227,9 @@ def test_str():
218227
219228
In other words, each entry ``out[i,j,...,:]`` is an N-dimensional
220229
value drawn from the distribution.
230+
list of str
231+
This is not a real return value. It exists to test
232+
anonymous return values.
221233
222234
Other Parameters
223235
----------------
@@ -226,12 +238,12 @@ def test_str():
226238
227239
Raises
228240
------
229-
RuntimeError :
241+
RuntimeError
230242
Some error
231243
232244
Warns
233245
-----
234-
RuntimeWarning :
246+
RuntimeWarning
235247
Some warning
236248
237249
Warnings
@@ -334,6 +346,11 @@ def test_sphinx_str():
334346
In other words, each entry ``out[i,j,...,:]`` is an N-dimensional
335347
value drawn from the distribution.
336348
349+
list of str
350+
351+
This is not a real return value. It exists to test
352+
anonymous return values.
353+
337354
:Other Parameters:
338355
339356
**spam** : parrot
@@ -342,13 +359,13 @@ def test_sphinx_str():
342359
343360
:Raises:
344361
345-
**RuntimeError** :
362+
**RuntimeError**
346363
347364
Some error
348365
349366
:Warns:
350367
351-
**RuntimeWarning** :
368+
**RuntimeWarning**
352369
353370
Some warning
354371
@@ -698,11 +715,11 @@ def test_class_members_doc():
698715
699716
Methods
700717
-------
701-
a :
718+
a
702719
703-
b :
720+
b
704721
705-
c :
722+
c
706723
707724
.. index::
708725

0 commit comments

Comments
 (0)