Skip to content

Commit d49deef

Browse files
authored
Support Receives section for generator.send(...) params (#145)
1 parent c8513c5 commit d49deef

File tree

5 files changed

+95
-11
lines changed

5 files changed

+95
-11
lines changed

doc/format.rst

+16-8
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,21 @@ The sections of a function's docstring are:
252252
Support for the **Yields** section was added in `numpydoc
253253
<https://github.com/numpy/numpydoc>`_ version 0.6.
254254

255-
7. **Other Parameters**
255+
7. **Receives**
256+
257+
Explanation of parameters passed to a generator's ``.send()`` method,
258+
formatted as for Parameters, above. Since, like for Yields and Returns, a
259+
single object is always passed to the method, this may describe either the
260+
single parameter, or positional arguments passed as a tuple. If a docstring
261+
includes Receives it must also include Yields.
262+
263+
8. **Other Parameters**
256264

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

261-
8. **Raises**
269+
9. **Raises**
262270

263271
An optional section detailing which errors get raised and under
264272
what conditions::
@@ -271,16 +279,16 @@ The sections of a function's docstring are:
271279
This section should be used judiciously, i.e., only for errors
272280
that are non-obvious or have a large chance of getting raised.
273281

274-
9. **Warns**
282+
10. **Warns**
275283

276284
An optional section detailing which warnings get raised and
277285
under what conditions, formatted similarly to Raises.
278286

279-
10. **Warnings**
287+
11. **Warnings**
280288

281289
An optional section with cautions to the user in free text/reST.
282290

283-
11. **See Also**
291+
12. **See Also**
284292

285293
An optional section used to refer to related code. This section
286294
can be very useful, but should be used judiciously. The goal is to
@@ -319,7 +327,7 @@ The sections of a function's docstring are:
319327
func_b, func_c_, func_d
320328
func_e
321329

322-
12. **Notes**
330+
13. **Notes**
323331

324332
An optional section that provides additional information about the
325333
code, possibly including a discussion of the algorithm. This
@@ -364,7 +372,7 @@ The sections of a function's docstring are:
364372
where filename is a path relative to the reference guide source
365373
directory.
366374

367-
13. **References**
375+
14. **References**
368376

369377
References cited in the **notes** section may be listed here,
370378
e.g. if you cited the article below using the text ``[1]_``,
@@ -397,7 +405,7 @@ The sections of a function's docstring are:
397405

398406
.. highlight:: pycon
399407

400-
14. **Examples**
408+
15. **Examples**
401409

402410
An optional section for examples, using the `doctest
403411
<http://docs.python.org/library/doctest.html>`_ format.

numpydoc/docscrape.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ class NumpyDocString(Mapping):
127127
'Parameters': [],
128128
'Returns': [],
129129
'Yields': [],
130+
'Receives': [],
130131
'Raises': [],
131132
'Warns': [],
132133
'Other Parameters': [],
@@ -350,6 +351,9 @@ def _parse(self):
350351
if has_returns and has_yields:
351352
msg = 'Docstring contains both a Returns and Yields section.'
352353
raise ValueError(msg)
354+
if not has_yields and 'Receives' in section_names:
355+
msg = 'Docstring contains a Receives section but not Yields.'
356+
raise ValueError(msg)
353357

354358
for (section, content) in sections:
355359
if not section.startswith('..'):
@@ -359,8 +363,8 @@ def _parse(self):
359363
self._error_location("The section %s appears twice"
360364
% section)
361365

362-
if section in ('Parameters', 'Returns', 'Yields', 'Raises',
363-
'Warns', 'Other Parameters', 'Attributes',
366+
if section in ('Parameters', 'Returns', 'Yields', 'Receives',
367+
'Raises', 'Warns', 'Other Parameters', 'Attributes',
364368
'Methods'):
365369
self[section] = self._parse_param_list(content)
366370
elif section.startswith('.. index::'):
@@ -484,7 +488,7 @@ def __str__(self, func_role=''):
484488
out += self._str_signature()
485489
out += self._str_summary()
486490
out += self._str_extended_summary()
487-
for param_list in ('Parameters', 'Returns', 'Yields',
491+
for param_list in ('Parameters', 'Returns', 'Yields', 'Receives',
488492
'Other Parameters', 'Raises', 'Warns'):
489493
out += self._str_param_list(param_list)
490494
out += self._str_section('Warnings')

numpydoc/docscrape_sphinx.py

+1
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ def __str__(self, indent=0, func_role="obj"):
374374
'parameters': self._str_param_list('Parameters'),
375375
'returns': self._str_returns('Returns'),
376376
'yields': self._str_returns('Yields'),
377+
'receives': self._str_returns('Receives'),
377378
'other_parameters': self._str_param_list('Other Parameters'),
378379
'raises': self._str_param_list('Raises'),
379380
'warns': self._str_param_list('Warns'),

numpydoc/templates/numpydoc_docstring.rst

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
{{parameters}}
55
{{returns}}
66
{{yields}}
7+
{{receives}}
78
{{other_parameters}}
89
{{raises}}
910
{{warns}}

numpydoc/tests/test_docscrape.py

+70
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,25 @@
150150
doc_yields = NumpyDocString(doc_yields_txt)
151151

152152

153+
doc_sent_txt = """
154+
Test generator
155+
156+
Yields
157+
------
158+
a : int
159+
The number of apples.
160+
161+
Receives
162+
--------
163+
b : int
164+
The number of bananas.
165+
c : int
166+
The number of oranges.
167+
168+
"""
169+
doc_sent = NumpyDocString(doc_sent_txt)
170+
171+
153172
def test_signature():
154173
assert doc['Signature'].startswith('numpy.multivariate_normal(')
155174
assert doc['Signature'].endswith('spam=None)')
@@ -216,6 +235,38 @@ def test_yields():
216235
assert desc[0].endswith(end)
217236

218237

238+
def test_sent():
239+
section = doc_sent['Receives']
240+
assert len(section) == 2
241+
truth = [('b', 'int', 'bananas.'),
242+
('c', 'int', 'oranges.')]
243+
for (arg, arg_type, desc), (arg_, arg_type_, end) in zip(section, truth):
244+
assert arg == arg_
245+
assert arg_type == arg_type_
246+
assert desc[0].startswith('The number of')
247+
assert desc[0].endswith(end)
248+
249+
250+
def test_returnyield():
251+
doc_text = """
252+
Test having returns and yields.
253+
254+
Returns
255+
-------
256+
int
257+
The number of apples.
258+
259+
Yields
260+
------
261+
a : int
262+
The number of apples.
263+
b : int
264+
The number of bananas.
265+
266+
"""
267+
assert_raises(ValueError, NumpyDocString, doc_text)
268+
269+
219270
def test_returnyield():
220271
doc_text = """
221272
Test having returns and yields.
@@ -468,6 +519,25 @@ def test_yield_str():
468519
.. index:: """)
469520

470521

522+
def test_receives_str():
523+
line_by_line_compare(str(doc_sent),
524+
"""Test generator
525+
526+
Yields
527+
------
528+
a : int
529+
The number of apples.
530+
531+
Receives
532+
--------
533+
b : int
534+
The number of bananas.
535+
c : int
536+
The number of oranges.
537+
538+
.. index:: """)
539+
540+
471541
def test_no_index_in_str():
472542
assert "index" not in str(NumpyDocString("""Test idx
473543

0 commit comments

Comments
 (0)