Skip to content

DOC: Validate in docstrings that numpy and pandas are not imported #23161

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

Merged
merged 13 commits into from
Nov 4, 2018
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
13 changes: 13 additions & 0 deletions scripts/validate_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,12 @@ def examples_errors(self):
error_msgs += f.getvalue()
return error_msgs

@property
def examples_source_code(self):
codes = doctest.DocTestParser().get_examples(self.raw_doc)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
codes = doctest.DocTestParser().get_examples(self.raw_doc)
lines = doctest.DocTestParser().get_examples(self.raw_doc)

codes = [line.source for line in codes]
return codes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also return in the previous line.

May be lines is a better name? codes sounds to me like identifiers



def validate_one(func_name):
"""
Expand Down Expand Up @@ -531,6 +537,13 @@ def validate_one(func_name):
examples_errs = doc.examples_errors
if examples_errs:
errs.append('Examples do not pass tests')
examples_source_code = doc.examples_source_code
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be you can do the join here, so doesn't need to be repeated? also, is the lines end with \n I'd join by '', otherwise by '\n'. Doesn't change the result, but conceptually the content of the variable is something that makes more sense.

if 'import numpy' in ' '.join(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 ' '.join(examples_source_code):
errs.append("Pandas does not need to be imported in the examples, "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't usually use capitalized pandas, even at the beginning of sentence. And in this case, as it's more the name of the module than the project, I'd use lower case also for numpy.

"as it's assumed to be already imported as pd")

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