Skip to content

Commit e97067f

Browse files
committed
Upgrading remove_returns and prepend_docstr_noreturns to remove_sections and prepend_docstr_nosections.
The new functions remove and prepend a list of sections rather than just “Returns”
1 parent 674f926 commit e97067f

3 files changed

Lines changed: 44 additions & 15 deletions

File tree

astroquery/utils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
from .class_or_instance import *
1111
from .commons import *
1212
from .process_asyncs import async_to_sync
13-
from .docstr_chompers import prepend_docstr_noreturns
13+
from .docstr_chompers import prepend_docstr_nosections

astroquery/utils/docstr_chompers.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@ def dec(fn):
1010
return dec
1111

1212

13-
def prepend_docstr_noreturns(doc):
13+
def prepend_docstr_nosections(doc, sections=['Returns', ]):
1414
"""
1515
Decorator to prepend to the function's docstr after stripping out the
16-
"Returns".
16+
list of sections provided (by default "Returns" only).
1717
"""
1818
def dec(fn):
19-
fn.__doc__ = ("\n".join(remove_returns(doc)) +
19+
fn.__doc__ = ("\n".join(remove_sections(doc, sections)) +
2020
textwrap.dedent(fn.__doc__))
2121
return fn
2222
return dec
2323

2424

25-
def remove_returns(doc):
25+
def remove_sections(doc, sections):
2626
"""
27-
Given a numpy-formatted docstring, remove the "Returns" block
28-
and dedent the whole thing.
27+
Given a numpy-formatted docstring, remove the section blocks provided in
28+
``sections`` and dedent the whole thing.
2929
3030
Returns
3131
-------
@@ -37,7 +37,7 @@ def remove_returns(doc):
3737
rblock = False
3838
for line in lines:
3939
lstrip = line.rstrip()
40-
if lstrip == "Returns":
40+
if lstrip in sections:
4141
rblock = True
4242
continue
4343
elif rblock:

astroquery/utils/tests/test_utils.py

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
from ...utils import chunk_read, chunk_report, class_or_instance, commons
2121
from ...utils.process_asyncs import async_to_sync_docstr, async_to_sync
22-
from ...utils.docstr_chompers import remove_returns, prepend_docstr_noreturns
22+
from ...utils.docstr_chompers import remove_sections, prepend_docstr_nosections
2323

2424

2525
class SimpleQueryClass(object):
@@ -284,6 +284,10 @@ def test_async_to_sync(cls=Dummy):
284284

285285

286286
docstr3 = """
287+
Parameters
288+
----------
289+
first_param
290+
287291
Returns
288292
-------
289293
Nothing!
@@ -301,10 +305,12 @@ def test_async_to_sync(cls=Dummy):
301305

302306

303307
def test_return_chomper(doc=docstr3, out=docstr3_out):
304-
assert remove_returns(doc) == [x.lstrip() for x in out.split('\n')]
308+
assert (remove_sections(doc, sections=['Returns', 'Parameters']) ==
309+
[x.lstrip() for x in out.split('\n')])
310+
305311

306312

307-
def dummyfunc():
313+
def dummyfunc1():
308314
"""
309315
Returns
310316
-------
@@ -317,15 +323,28 @@ def dummyfunc():
317323
pass
318324

319325

326+
def dummyfunc2():
327+
"""
328+
Returns
329+
-------
330+
Nothing!
331+
"""
332+
pass
333+
334+
320335
docstr4 = """
321336
Blah Blah Blah
322337
323338
Returns
324339
-------
325340
nothing
341+
342+
Examples
343+
--------
344+
no_examples_at_all
326345
"""
327346

328-
docstr4_out = """
347+
docstr4_out1 = """
329348
Blah Blah Blah
330349
331350
Returns
@@ -337,10 +356,20 @@ def dummyfunc():
337356
Nada
338357
"""
339358

359+
docstr4_out2 = """
360+
Blah Blah Blah
361+
362+
Returns
363+
-------
364+
Nothing!
365+
"""
366+
340367

341-
def test_prepend_docstr(doc=docstr4, func=dummyfunc, out=docstr4_out):
342-
fn = prepend_docstr_noreturns(doc)(func)
343-
assert fn.__doc__ == textwrap.dedent(docstr4_out)
368+
@pytest.mark.parametrize("func, out", [(dummyfunc1, docstr4_out1),
369+
(dummyfunc2, docstr4_out2)])
370+
def test_prepend_docstr(func, out, doc=docstr4):
371+
fn = prepend_docstr_nosections(doc, sections=['Returns', 'Examples'])(func)
372+
assert fn.__doc__ == textwrap.dedent(out)
344373

345374

346375
@async_to_sync

0 commit comments

Comments
 (0)