Skip to content

Commit 2a0fe75

Browse files
timhoffmrgommers
authored andcommitted
FIX parsing of type-only return params
1 parent 8f1ac50 commit 2a0fe75

File tree

3 files changed

+46
-36
lines changed

3 files changed

+46
-36
lines changed

numpydoc/docscrape.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,18 @@ def _read_sections(self):
220220
else:
221221
yield name, self._strip(data[2:])
222222

223-
def _parse_param_list(self, content):
223+
def _parse_param_list(self, content, single_element_is_type=False):
224224
r = Reader(content)
225225
params = []
226226
while not r.eof():
227227
header = r.read().strip()
228228
if ' : ' in header:
229229
arg_name, arg_type = header.split(' : ')[:2]
230230
else:
231-
arg_name, arg_type = header, ''
231+
if single_element_is_type:
232+
arg_name, arg_type = '', header
233+
else:
234+
arg_name, arg_type = header, ''
232235

233236
desc = r.read_to_next_unindented_line()
234237
desc = dedent_lines(desc)
@@ -393,10 +396,12 @@ def _parse(self):
393396
self._error_location("The section %s appears twice"
394397
% section)
395398

396-
if section in ('Parameters', 'Returns', 'Yields', 'Receives',
397-
'Raises', 'Warns', 'Other Parameters', 'Attributes',
399+
if section in ('Parameters', 'Other Parameters', 'Attributes',
398400
'Methods'):
399401
self[section] = self._parse_param_list(content)
402+
elif section in ('Returns', 'Yields', 'Raises', 'Warns', 'Receives'):
403+
self[section] = self._parse_param_list(
404+
content, single_element_is_type=True)
400405
elif section.startswith('.. index::'):
401406
self['index'] = self._parse_index(section, content)
402407
elif section == 'See Also':
@@ -452,10 +457,12 @@ def _str_param_list(self, name):
452457
if self[name]:
453458
out += self._str_header(name)
454459
for param in self[name]:
460+
parts = []
461+
if param.name:
462+
parts.append(param.name)
455463
if param.type:
456-
out += ['%s : %s' % (param.name, param.type)]
457-
else:
458-
out += [param.name]
464+
parts.append(param.type)
465+
out += [' : '.join(parts)]
459466
if param.desc and ''.join(param.desc).strip():
460467
out += self._str_indent(param.desc)
461468
out += ['']
@@ -637,7 +644,7 @@ def __init__(self, cls, doc=None, modulename='', func_doc=FunctionDoc,
637644
if _members is ALL:
638645
_members = None
639646
_exclude = config.get('exclude-members', [])
640-
647+
641648
if config.get('show_class_members', True) and _exclude is not ALL:
642649
def splitlines_x(s):
643650
if not s:
@@ -649,7 +656,7 @@ def splitlines_x(s):
649656
if not self[field]:
650657
doc_list = []
651658
for name in sorted(items):
652-
if (name in _exclude or
659+
if (name in _exclude or
653660
(_members and name not in _members)):
654661
continue
655662
try:

numpydoc/docscrape_sphinx.py

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

7272
def _str_returns(self, name='Returns'):
73-
typed_fmt = '**%s** : %s'
74-
untyped_fmt = '**%s**'
73+
named_fmt = '**%s** : %s'
74+
unnamed_fmt = '%s'
7575

7676
out = []
7777
if self[name]:
7878
out += self._str_field_list(name)
7979
out += ['']
8080
for param in self[name]:
81-
if param.type:
82-
out += self._str_indent([typed_fmt % (param.name.strip(),
81+
if param.name:
82+
out += self._str_indent([named_fmt % (param.name.strip(),
8383
param.type)])
8484
else:
85-
out += self._str_indent([untyped_fmt % param.name.strip()])
85+
out += self._str_indent([unnamed_fmt % param.type.strip()])
8686
if not param.desc:
8787
out += self._str_indent(['..'], 8)
8888
else:
@@ -209,12 +209,13 @@ def _str_param_list(self, name, fake_autosummary=False):
209209
display_param, desc = self._process_param(param.name,
210210
param.desc,
211211
fake_autosummary)
212-
212+
parts = []
213+
if display_param:
214+
parts.append(display_param)
213215
if param.type:
214-
out += self._str_indent(['%s : %s' % (display_param,
215-
param.type)])
216-
else:
217-
out += self._str_indent([display_param])
216+
parts.append(param.type)
217+
out += self._str_indent([' : '.join(parts)])
218+
218219
if desc and self.use_blockquotes:
219220
out += ['']
220221
elif not desc:
@@ -376,8 +377,8 @@ def __str__(self, indent=0, func_role="obj"):
376377
'yields': self._str_returns('Yields'),
377378
'receives': self._str_returns('Receives'),
378379
'other_parameters': self._str_param_list('Other Parameters'),
379-
'raises': self._str_param_list('Raises'),
380-
'warns': self._str_param_list('Warns'),
380+
'raises': self._str_returns('Raises'),
381+
'warns': self._str_returns('Warns'),
381382
'warnings': self._str_warnings(),
382383
'see_also': self._str_see_also(func_role),
383384
'notes': self._str_section('Notes'),

numpydoc/tests/test_docscrape.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ def test_returns():
217217
assert desc[-1].endswith('anonymous return values.')
218218

219219
arg, arg_type, desc = doc['Returns'][2]
220-
assert arg == 'no_description'
221-
assert arg_type == ''
220+
assert arg == ''
221+
assert arg_type == 'no_description'
222222
assert not ''.join(desc).strip()
223223

224224

@@ -227,7 +227,7 @@ def test_yields():
227227
assert len(section) == 3
228228
truth = [('a', 'int', 'apples.'),
229229
('b', 'int', 'bananas.'),
230-
('int', '', 'unknowns.')]
230+
('', 'int', 'unknowns.')]
231231
for (arg, arg_type, desc), (arg_, arg_type_, end) in zip(section, truth):
232232
assert arg == arg_
233233
assert arg_type == arg_type_
@@ -594,11 +594,11 @@ def test_sphinx_str():
594594
In other words, each entry ``out[i,j,...,:]`` is an N-dimensional
595595
value drawn from the distribution.
596596
597-
**list of str**
597+
list of str
598598
This is not a real return value. It exists to test
599599
anonymous return values.
600600
601-
**no_description**
601+
no_description
602602
..
603603
604604
:Other Parameters:
@@ -608,12 +608,12 @@ def test_sphinx_str():
608608
609609
:Raises:
610610
611-
**RuntimeError**
611+
RuntimeError
612612
Some error
613613
614614
:Warns:
615615
616-
**RuntimeWarning**
616+
RuntimeWarning
617617
Some warning
618618
619619
.. warning::
@@ -687,7 +687,7 @@ def test_sphinx_yields_str():
687687
**b** : int
688688
The number of bananas.
689689
690-
**int**
690+
int
691691
The number of unknowns.
692692
""")
693693

@@ -754,16 +754,18 @@ def test_empty_extended_summary():
754754

755755
def test_raises():
756756
assert len(doc5['Raises']) == 1
757-
name, _, desc = doc5['Raises'][0]
758-
assert name == 'LinAlgException'
759-
assert desc == ['If array is singular.']
757+
param = doc5['Raises'][0]
758+
assert param.name == ''
759+
assert param.type == 'LinAlgException'
760+
assert param.desc == 'If array is singular.'
760761

761762

762763
def test_warns():
763764
assert len(doc5['Warns']) == 1
764-
name, _, desc = doc5['Warns'][0]
765-
assert name == 'SomeWarning'
766-
assert desc == ['If needed']
765+
param = doc5['Warns'][0]
766+
assert param.name == ''
767+
assert param.type == 'SomeWarning'
768+
assert param.desc == 'If needed'
767769

768770

769771
def test_see_also():
@@ -995,7 +997,7 @@ def test_use_blockquotes():
995997
996998
GHI
997999
998-
**JKL**
1000+
JKL
9991001
10001002
MNO
10011003
''')

0 commit comments

Comments
 (0)