diff --git a/scripts/tests/test_validate_docstrings.py b/scripts/tests/test_validate_docstrings.py index 27c63e3ba3a79..01d7b0b4bd3bb 100644 --- a/scripts/tests/test_validate_docstrings.py +++ b/scripts/tests/test_validate_docstrings.py @@ -502,6 +502,20 @@ def no_punctuation(self): return "Hello world!" +class BadExamples(object): + + def npPd_import(self): + """ + Provide example with numpy and pandas import + + Examples + -------- + import numpy as np + import pandas as pd + """ + pass + + class TestValidator(object): def _import_path(self, klass=None, func=None): @@ -600,7 +614,11 @@ def test_bad_generic_functions(self, func): pytest.param('BadReturns', 'no_description', ('foo',), marks=pytest.mark.xfail), pytest.param('BadReturns', 'no_punctuation', ('foo',), - marks=pytest.mark.xfail) + marks=pytest.mark.xfail), + # Examples + ('BadExamples', 'npPd_import', + ('Examples should not have `import pandas as pd` ', + 'Examples should not have `import numpy as np` ',)) ]) def test_bad_examples(self, capsys, klass, func, msgs): result = validate_one(self._import_path(klass=klass, func=func)) # noqa:F821 diff --git a/scripts/validate_docstrings.py b/scripts/validate_docstrings.py index 6588522331433..b58be4961595a 100755 --- a/scripts/validate_docstrings.py +++ b/scripts/validate_docstrings.py @@ -509,6 +509,12 @@ def validate_one(func_name): if not doc.examples: wrns.append('No examples section found') else: + if 'import numpy as np' in ' '.join(doc.examples): + errs.append(' Examples should not have ' + '`import numpy as np` ') + if 'import pandas as pd' in ' '.join(doc.examples): + errs.append(' Examples should not have ' + '`import pandas as pd` ') examples_errs = doc.examples_errors if examples_errs: errs.append('Examples do not pass tests')