Skip to content

Commit 4d858d0

Browse files
committed
FIX parsing of type-only return params
1 parent 136d4dd commit 4d858d0

File tree

3 files changed

+46
-36
lines changed

3 files changed

+46
-36
lines changed

numpydoc/docscrape.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,18 @@ def _read_sections(self):
214214
else:
215215
yield name, self._strip(data[2:])
216216

217-
def _parse_param_list(self, content):
217+
def _parse_param_list(self, content, single_element_is_type=False):
218218
r = Reader(content)
219219
params = []
220220
while not r.eof():
221221
header = r.read().strip()
222222
if ' : ' in header:
223223
arg_name, arg_type = header.split(' : ')[:2]
224224
else:
225-
arg_name, arg_type = header, ''
225+
if single_element_is_type:
226+
arg_name, arg_type = '', header
227+
else:
228+
arg_name, arg_type = header, ''
226229

227230
desc = r.read_to_next_unindented_line()
228231
desc = dedent_lines(desc)
@@ -354,10 +357,12 @@ def _parse(self):
354357
self._error_location("The section %s appears twice"
355358
% section)
356359

357-
if section in ('Parameters', 'Returns', 'Yields', 'Raises',
358-
'Warns', 'Other Parameters', 'Attributes',
360+
if section in ('Parameters', 'Other Parameters', 'Attributes',
359361
'Methods'):
360362
self[section] = self._parse_param_list(content)
363+
elif section in ('Returns', 'Yields', 'Raises', 'Warns'):
364+
self[section] = self._parse_param_list(
365+
content, single_element_is_type=True)
361366
elif section.startswith('.. index::'):
362367
self['index'] = self._parse_index(section, content)
363368
elif section == 'See Also':
@@ -413,10 +418,12 @@ def _str_param_list(self, name):
413418
if self[name]:
414419
out += self._str_header(name)
415420
for param in self[name]:
421+
parts = []
422+
if param.name:
423+
parts.append(param.name)
416424
if param.type:
417-
out += ['%s : %s' % (param.name, param.type)]
418-
else:
419-
out += [param.name]
425+
parts.append(param.type)
426+
out += [' : '.join(parts)]
420427
if param.desc and ''.join(param.desc).strip():
421428
out += self._str_indent(param.desc)
422429
out += ['']

numpydoc/docscrape_sphinx.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,19 @@ def _str_extended_summary(self):
6767
return self['Extended Summary'] + ['']
6868

6969
def _str_returns(self, name='Returns'):
70-
typed_fmt = '**%s** : %s'
71-
untyped_fmt = '**%s**'
70+
named_fmt = '**%s** : %s'
71+
unnamed_fmt = '%s'
7272

7373
out = []
7474
if self[name]:
7575
out += self._str_field_list(name)
7676
out += ['']
7777
for param in self[name]:
78-
if param.type:
79-
out += self._str_indent([typed_fmt % (param.name.strip(),
78+
if param.name:
79+
out += self._str_indent([named_fmt % (param.name.strip(),
8080
param.type)])
8181
else:
82-
out += self._str_indent([untyped_fmt % param.name.strip()])
82+
out += self._str_indent([unnamed_fmt % param.type.strip()])
8383
if not param.desc:
8484
out += self._str_indent(['..'], 8)
8585
else:
@@ -205,12 +205,13 @@ def _str_param_list(self, name, fake_autosummary=False):
205205
display_param, desc = self._process_param(param.name,
206206
param.desc,
207207
fake_autosummary)
208-
208+
parts = []
209+
if display_param:
210+
parts.append(display_param)
209211
if param.type:
210-
out += self._str_indent(['%s : %s' % (display_param,
211-
param.type)])
212-
else:
213-
out += self._str_indent([display_param])
212+
parts.append(param.type)
213+
out += self._str_indent([' : '.join(parts)])
214+
214215
if desc and self.use_blockquotes:
215216
out += ['']
216217
elif not desc:
@@ -371,8 +372,8 @@ def __str__(self, indent=0, func_role="obj"):
371372
'returns': self._str_returns('Returns'),
372373
'yields': self._str_returns('Yields'),
373374
'other_parameters': self._str_param_list('Other Parameters'),
374-
'raises': self._str_param_list('Raises'),
375-
'warns': self._str_param_list('Warns'),
375+
'raises': self._str_returns('Raises'),
376+
'warns': self._str_returns('Warns'),
376377
'warnings': self._str_warnings(),
377378
'see_also': self._str_see_also(func_role),
378379
'notes': self._str_section('Notes'),

numpydoc/tests/test_docscrape.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,14 @@ def test_returns():
193193
assert desc[-1].endswith('distribution.')
194194

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

201201
arg, arg_type, desc = doc['Returns'][2]
202-
assert_equal(arg, 'no_description')
203-
assert_equal(arg_type, '')
202+
assert_equal(arg, '')
203+
assert_equal(arg_type, 'no_description')
204204
assert not ''.join(desc).strip()
205205

206206

@@ -209,7 +209,7 @@ def test_yields():
209209
assert_equal(len(section), 3)
210210
truth = [('a', 'int', 'apples.'),
211211
('b', 'int', 'bananas.'),
212-
('int', '', 'unknowns.')]
212+
('', 'int', 'unknowns.')]
213213
for (arg, arg_type, desc), (arg_, arg_type_, end) in zip(section, truth):
214214
assert_equal(arg, arg_)
215215
assert_equal(arg_type, arg_type_)
@@ -509,11 +509,11 @@ def test_sphinx_str():
509509
In other words, each entry ``out[i,j,...,:]`` is an N-dimensional
510510
value drawn from the distribution.
511511
512-
**list of str**
512+
list of str
513513
This is not a real return value. It exists to test
514514
anonymous return values.
515515
516-
**no_description**
516+
no_description
517517
..
518518
519519
:Other Parameters:
@@ -523,12 +523,12 @@ def test_sphinx_str():
523523
524524
:Raises:
525525
526-
**RuntimeError**
526+
RuntimeError
527527
Some error
528528
529529
:Warns:
530530
531-
**RuntimeWarning**
531+
RuntimeWarning
532532
Some warning
533533
534534
.. warning::
@@ -602,7 +602,7 @@ def test_sphinx_yields_str():
602602
**b** : int
603603
The number of bananas.
604604
605-
**int**
605+
int
606606
The number of unknowns.
607607
""")
608608

@@ -669,16 +669,18 @@ def test_empty_extended_summary():
669669

670670
def test_raises():
671671
assert_equal(len(doc5['Raises']), 1)
672-
name,_,desc = doc5['Raises'][0]
673-
assert_equal(name,'LinAlgException')
674-
assert_equal(desc,['If array is singular.'])
672+
param = doc5['Raises'][0]
673+
assert_equal(param.name, '')
674+
assert_equal(param.type, 'LinAlgException')
675+
assert_equal(param.desc, ['If array is singular.'])
675676

676677

677678
def test_warns():
678679
assert_equal(len(doc5['Warns']), 1)
679-
name,_,desc = doc5['Warns'][0]
680-
assert_equal(name,'SomeWarning')
681-
assert_equal(desc,['If needed'])
680+
param = doc5['Warns'][0]
681+
assert_equal(param.name, '')
682+
assert_equal(param.type, 'SomeWarning')
683+
assert_equal(param.desc, ['If needed'])
682684

683685

684686
def test_see_also():
@@ -897,7 +899,7 @@ def test_use_blockquotes():
897899
898900
GHI
899901
900-
**JKL**
902+
JKL
901903
902904
MNO
903905
''')

0 commit comments

Comments
 (0)