Skip to content

Commit e0f9689

Browse files
committed
DOC: add checks on the returns section in the docstrings (pandas-dev#23138)
1 parent 0d6cb3c commit e0f9689

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

scripts/tests/test_validate_docstrings.py

+36-4
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,30 @@ def no_punctuation(self):
545545
"""
546546
return "Hello world!"
547547

548+
def named_single_return(self):
549+
"""
550+
Provides name but returns only one value.
551+
552+
Returns
553+
-------
554+
s : str
555+
A nice greeting.
556+
"""
557+
return "Hello world!"
558+
559+
def no_capitalization(self):
560+
"""
561+
Forgets capitalization in return values descriptions.
562+
563+
Returns
564+
-------
565+
foo : str
566+
the first returned string.
567+
bar : str
568+
the second returned string.
569+
"""
570+
return "Hello", "World!"
571+
548572

549573
class BadSeeAlso(object):
550574

@@ -696,10 +720,18 @@ def test_bad_generic_functions(self, func):
696720
('BadReturns', 'yield_not_documented', ('No Yields section found',)),
697721
pytest.param('BadReturns', 'no_type', ('foo',),
698722
marks=pytest.mark.xfail),
699-
pytest.param('BadReturns', 'no_description', ('foo',),
700-
marks=pytest.mark.xfail),
701-
pytest.param('BadReturns', 'no_punctuation', ('foo',),
702-
marks=pytest.mark.xfail),
723+
('BadReturns', 'no_description',
724+
('Return value has no description',)),
725+
('BadReturns', 'no_punctuation',
726+
('Return value description should finish with "."',)),
727+
('BadReturns', 'named_single_return',
728+
('No name is to be provided when returning a single value',)),
729+
('BadReturns', 'no_capitalization',
730+
('Return value "foo" description should start with a capital '
731+
'letter',)),
732+
('BadReturns', 'no_capitalization',
733+
('Return value "bar" description should start with a capital '
734+
'letter',)),
703735
# See Also tests
704736
('BadSeeAlso', 'prefix_pandas',
705737
('pandas.Series.rename in `See Also` section '

scripts/validate_docstrings.py

+28-2
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,34 @@ def validate_one(func_name):
491491
errs.append('\t{}'.format(param_err))
492492

493493
if doc.is_function_or_method:
494-
if not doc.returns and "return" in doc.method_source:
495-
errs.append('No Returns section found')
494+
if not doc.returns:
495+
if "return" in doc.method_source:
496+
errs.append('No Returns section found')
497+
else:
498+
returns_errs = []
499+
if len(doc.returns) == 1 and doc.returns[0][1]:
500+
returns_errs.append('No name is to be provided when '
501+
'returning a single value.')
502+
for name, type_, desc in doc.returns:
503+
desc = ''.join(desc)
504+
name = '"' + name + '" ' if type_ else ''
505+
if not desc:
506+
returns_errs.append('Return value {}has no '
507+
'description'.format(name))
508+
else:
509+
if not desc[0].isupper():
510+
returns_errs.append('Return value {}description '
511+
'should start with a capital '
512+
'letter'.format(name))
513+
if desc[-1] != '.':
514+
returns_errs.append('Return value {}description '
515+
'should finish with '
516+
'"."'.format(name))
517+
if returns_errs:
518+
errs.append('Errors in returns section')
519+
for returns_err in returns_errs:
520+
errs.append('\t{}'.format(returns_err))
521+
496522
if not doc.yields and "yield" in doc.method_source:
497523
errs.append('No Yields section found')
498524

0 commit comments

Comments
 (0)