Skip to content

Commit 6df8567

Browse files
igorfassenjreback
authored andcommitted
DOC: add checks on the returns section in the docstrings (#23138) (#23432)
1 parent 0769688 commit 6df8567

File tree

2 files changed

+70
-6
lines changed

2 files changed

+70
-6
lines changed

scripts/tests/test_validate_docstrings.py

+49-4
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,43 @@ def no_punctuation(self):
633633
"""
634634
return "Hello world!"
635635

636+
def named_single_return(self):
637+
"""
638+
Provides name but returns only one value.
639+
640+
Returns
641+
-------
642+
s : str
643+
A nice greeting.
644+
"""
645+
return "Hello world!"
646+
647+
def no_capitalization(self):
648+
"""
649+
Forgets capitalization in return values description.
650+
651+
Returns
652+
-------
653+
foo : str
654+
The first returned string.
655+
bar : str
656+
the second returned string.
657+
"""
658+
return "Hello", "World!"
659+
660+
def no_period_multi(self):
661+
"""
662+
Forgets period in return values description.
663+
664+
Returns
665+
-------
666+
foo : str
667+
The first returned string
668+
bar : str
669+
The second returned string.
670+
"""
671+
return "Hello", "World!"
672+
636673

637674
class BadSeeAlso(object):
638675

@@ -829,10 +866,18 @@ def test_bad_generic_functions(self, capsys, func):
829866
('BadReturns', 'yield_not_documented', ('No Yields section found',)),
830867
pytest.param('BadReturns', 'no_type', ('foo',),
831868
marks=pytest.mark.xfail),
832-
pytest.param('BadReturns', 'no_description', ('foo',),
833-
marks=pytest.mark.xfail),
834-
pytest.param('BadReturns', 'no_punctuation', ('foo',),
835-
marks=pytest.mark.xfail),
869+
('BadReturns', 'no_description',
870+
('Return value has no description',)),
871+
('BadReturns', 'no_punctuation',
872+
('Return value description should finish with "."',)),
873+
('BadReturns', 'named_single_return',
874+
('The first line of the Returns section should contain only the '
875+
'type, unless multiple values are being returned',)),
876+
('BadReturns', 'no_capitalization',
877+
('Return value description should start with a capital '
878+
'letter',)),
879+
('BadReturns', 'no_period_multi',
880+
('Return value description should finish with "."',)),
836881
# Examples tests
837882
('BadGenericDocStrings', 'method',
838883
('Do not import numpy, as it is imported automatically',)),

scripts/validate_docstrings.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@
105105
'PR10': 'Parameter "{param_name}" requires a space before the colon '
106106
'separating the parameter name and type',
107107
'RT01': 'No Returns section found',
108+
'RT02': 'The first line of the Returns section should contain only the '
109+
'type, unless multiple values are being returned',
110+
'RT03': 'Return value has no description',
111+
'RT04': 'Return value description should start with a capital letter',
112+
'RT05': 'Return value description should finish with "."',
108113
'YD01': 'No Yields section found',
109114
'SA01': 'See Also section not found',
110115
'SA02': 'Missing period at end of description for See Also '
@@ -685,8 +690,22 @@ def get_validation_data(doc):
685690
errs.append(error('PR09', param_name=param))
686691

687692
if doc.is_function_or_method:
688-
if not doc.returns and 'return' in doc.method_source:
689-
errs.append(error('RT01'))
693+
if not doc.returns:
694+
if 'return' in doc.method_source:
695+
errs.append(error('RT01'))
696+
else:
697+
if len(doc.returns) == 1 and doc.returns[0][1]:
698+
errs.append(error('RT02'))
699+
for name_or_type, type_, desc in doc.returns:
700+
if not desc:
701+
errs.append(error('RT03'))
702+
else:
703+
desc = ' '.join(desc)
704+
if not desc[0].isupper():
705+
errs.append(error('RT04'))
706+
if not desc.endswith('.'):
707+
errs.append(error('RT05'))
708+
690709
if not doc.yields and 'yield' in doc.method_source:
691710
errs.append(error('YD01'))
692711

0 commit comments

Comments
 (0)