Skip to content

Commit 87fff99

Browse files
committed
Merge pull request #25 from chebee7i/patch-1
ENH: support Yields section
2 parents 51982b2 + 736951e commit 87fff99

File tree

4 files changed

+110
-11
lines changed

4 files changed

+110
-11
lines changed

numpydoc/docscrape.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ def __init__(self, docstring, config={}):
9797
'Extended Summary': [],
9898
'Parameters': [],
9999
'Returns': [],
100+
'Yields': [],
100101
'Raises': [],
101102
'Warns': [],
102103
'Other Parameters': [],
@@ -288,11 +289,22 @@ def _parse(self):
288289
self._doc.reset()
289290
self._parse_summary()
290291

291-
for (section,content) in self._read_sections():
292+
sections = list(self._read_sections())
293+
section_names = set([section for section, content in sections])
294+
295+
has_returns = 'Returns' in section_names
296+
has_yields = 'Yields' in section_names
297+
# We could do more tests, but we are not. Arbitrarily.
298+
if has_returns and has_yields:
299+
msg = 'Docstring contains both a Returns and Yields section.'
300+
raise ValueError(msg)
301+
302+
for (section, content) in sections:
292303
if not section.startswith('..'):
293304
section = ' '.join([s.capitalize() for s in section.split(' ')])
294-
if section in ('Parameters', 'Returns', 'Raises', 'Warns',
295-
'Other Parameters', 'Attributes', 'Methods'):
305+
if section in ('Parameters', 'Returns', 'Yields', 'Raises',
306+
'Warns', 'Other Parameters', 'Attributes',
307+
'Methods'):
296308
self[section] = self._parse_param_list(content)
297309
elif section.startswith('.. index::'):
298310
self['index'] = self._parse_index(section, content)
@@ -391,8 +403,8 @@ def __str__(self, func_role=''):
391403
out += self._str_signature()
392404
out += self._str_summary()
393405
out += self._str_extended_summary()
394-
for param_list in ('Parameters', 'Returns', 'Other Parameters',
395-
'Raises', 'Warns'):
406+
for param_list in ('Parameters', 'Returns', 'Yields',
407+
'Other Parameters', 'Raises', 'Warns'):
396408
out += self._str_param_list(param_list)
397409
out += self._str_section('Warnings')
398410
out += self._str_see_also(func_role)

numpydoc/docscrape_sphinx.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ def _str_summary(self):
4646
def _str_extended_summary(self):
4747
return self['Extended Summary'] + ['']
4848

49-
def _str_returns(self):
49+
def _str_returns(self, name='Returns'):
5050
out = []
51-
if self['Returns']:
52-
out += self._str_field_list('Returns')
51+
if self[name]:
52+
out += self._str_field_list(name)
5353
out += ['']
54-
for param, param_type, desc in self['Returns']:
54+
for param, param_type, desc in self[name]:
5555
if param_type:
5656
out += self._str_indent(['**%s** : %s' % (param.strip(),
5757
param_type)])
@@ -224,7 +224,8 @@ def __str__(self, indent=0, func_role="obj"):
224224
out += self._str_summary()
225225
out += self._str_extended_summary()
226226
out += self._str_param_list('Parameters')
227-
out += self._str_returns()
227+
out += self._str_returns('Returns')
228+
out += self._str_returns('Yields')
228229
for param_list in ('Other Parameters', 'Raises', 'Warns'):
229230
out += self._str_param_list(param_list)
230231
out += self._str_warnings()

numpydoc/tests/test_docscrape.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,20 @@
122122
'''
123123
doc = NumpyDocString(doc_txt)
124124

125+
doc_yields_txt = """
126+
Test generator
127+
128+
Yields
129+
------
130+
a : int
131+
The number of apples.
132+
b : int
133+
The number of bananas.
134+
int
135+
The number of unknowns.
136+
"""
137+
doc_yields = NumpyDocString(doc_yields_txt)
138+
125139

126140
def test_signature():
127141
assert doc['Signature'].startswith('numpy.multivariate_normal(')
@@ -164,6 +178,37 @@ def test_returns():
164178
assert desc[0].startswith('This is not a real')
165179
assert desc[-1].endswith('anonymous return values.')
166180

181+
def test_yields():
182+
section = doc_yields['Yields']
183+
assert_equal(len(section), 3)
184+
truth = [('a', 'int', 'apples.'),
185+
('b', 'int', 'bananas.'),
186+
('int', '', 'unknowns.')]
187+
for (arg, arg_type, desc), (arg_, arg_type_, end) in zip(section, truth):
188+
assert_equal(arg, arg_)
189+
assert_equal(arg_type, arg_type_)
190+
assert desc[0].startswith('The number of')
191+
assert desc[0].endswith(end)
192+
193+
def test_returnyield():
194+
doc_text = """
195+
Test having returns and yields.
196+
197+
Returns
198+
-------
199+
int
200+
The number of apples.
201+
202+
Yields
203+
------
204+
a : int
205+
The number of apples.
206+
b : int
207+
The number of bananas.
208+
209+
"""
210+
assert_raises(ValueError, NumpyDocString, doc_text)
211+
167212
def test_notes():
168213
assert doc['Notes'][0].startswith('Instead')
169214
assert doc['Notes'][-1].endswith('definite.')
@@ -193,6 +238,9 @@ def non_blank_line_by_line_compare(a,b):
193238
"\n>>> %s\n<<< %s\n" %
194239
(n,line,b[n]))
195240
def test_str():
241+
# doc_txt has the order of Notes and See Also sections flipped.
242+
# This should be handled automatically, and so, one thing this test does
243+
# is to make sure that See Also precedes Notes in the output.
196244
non_blank_line_by_line_compare(str(doc),
197245
"""numpy.multivariate_normal(mean, cov, shape=None, spam=None)
198246
@@ -302,6 +350,22 @@ def test_str():
302350
:refguide: random;distributions, random;gauss""")
303351

304352

353+
def test_yield_str():
354+
non_blank_line_by_line_compare(str(doc_yields),
355+
"""Test generator
356+
357+
Yields
358+
------
359+
a : int
360+
The number of apples.
361+
b : int
362+
The number of bananas.
363+
int
364+
The number of unknowns.
365+
366+
.. index:: """)
367+
368+
305369
def test_sphinx_str():
306370
sphinx_doc = SphinxDocString(doc_txt)
307371
non_blank_line_by_line_compare(str(sphinx_doc),
@@ -427,6 +491,27 @@ def test_sphinx_str():
427491
""")
428492

429493

494+
def test_sphinx_yields_str():
495+
sphinx_doc = SphinxDocString(doc_yields_txt)
496+
non_blank_line_by_line_compare(str(sphinx_doc),
497+
"""Test generator
498+
499+
:Yields:
500+
501+
**a** : int
502+
503+
The number of apples.
504+
505+
**b** : int
506+
507+
The number of bananas.
508+
509+
int
510+
511+
The number of unknowns.
512+
""")
513+
514+
430515
doc2 = NumpyDocString("""
431516
Returns array of indices of the maximum values of along the given axis.
432517

numpydoc/traitsdoc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def __init__(self, cls, modulename='', func_doc=SphinxFunctionDoc):
6161
'Extended Summary': [],
6262
'Parameters': [],
6363
'Returns': [],
64+
'Yields': [],
6465
'Raises': [],
6566
'Warns': [],
6667
'Other Parameters': [],
@@ -89,7 +90,7 @@ def __str__(self, indent=0, func_role="func"):
8990
out += self._str_summary()
9091
out += self._str_extended_summary()
9192
for param_list in ('Parameters', 'Traits', 'Methods',
92-
'Returns','Raises'):
93+
'Returns', 'Yields', 'Raises'):
9394
out += self._str_param_list(param_list)
9495
out += self._str_see_also("obj")
9596
out += self._str_section('Notes')

0 commit comments

Comments
 (0)