Skip to content

Check numpy and pandas import in examples #23160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion scripts/tests/test_validate_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions scripts/validate_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down