Skip to content
17 changes: 17 additions & 0 deletions scripts/tests/test_validate_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,18 @@ def mode(self, axis, numeric_only):
"""
pass

def good_imports(self):
"""
Ensure import other than numpy and pandas are fine.

Examples
--------
This example does not import pandas or import numpy.
>>> import time
>>> import datetime
"""
pass


class BadGenericDocStrings(object):
"""Everything here has a bad docstring
Expand Down Expand Up @@ -700,6 +712,11 @@ def test_bad_generic_functions(self, func):
marks=pytest.mark.xfail),
pytest.param('BadReturns', 'no_punctuation', ('foo',),
marks=pytest.mark.xfail),
# Examples tests
('BadGenericDocStrings', 'method',
('numpy does not need to be imported in the examples,')),
('BadGenericDocStrings', 'method',
('pandas does not need to be imported in the examples,')),
# See Also tests
('BadSeeAlso', 'prefix_pandas',
('pandas.Series.rename in `See Also` section '
Expand Down
12 changes: 12 additions & 0 deletions scripts/validate_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,11 @@ def examples_errors(self):
error_msgs += f.getvalue()
return error_msgs

@property
def examples_source_code(self):
lines = doctest.DocTestParser().get_examples(self.raw_doc)
return [line.source for line in lines]


def validate_one(func_name):
"""
Expand Down Expand Up @@ -531,6 +536,13 @@ def validate_one(func_name):
examples_errs = doc.examples_errors
if examples_errs:
errs.append('Examples do not pass tests')
examples_source_code = ''.join(doc.examples_source_code)
if 'import numpy' in examples_source_code:
errs.append("numpy does not need to be imported in the examples, "
"as it's assumed to be already imported as np")
if 'import pandas' in examples_source_code:
errs.append("pandas does not need to be imported in the examples, "
"as it's assumed to be already imported as pd")

return {'type': doc.type,
'docstring': doc.clean_doc,
Expand Down