diff --git a/scripts/tests/test_validate_docstrings.py b/scripts/tests/test_validate_docstrings.py index 0e10265a7291d..aa8a1500d9d3d 100644 --- a/scripts/tests/test_validate_docstrings.py +++ b/scripts/tests/test_validate_docstrings.py @@ -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 @@ -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 ' diff --git a/scripts/validate_docstrings.py b/scripts/validate_docstrings.py index 4b1834adcaf33..4c54762f6df31 100755 --- a/scripts/validate_docstrings.py +++ b/scripts/validate_docstrings.py @@ -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): """ @@ -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,