-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from 3 commits
8655fdd
5c071ec
5aff727
374c9ea
881b011
d824414
5d785c3
3e512ac
a2011b2
0243b91
d123b5c
ec78655
ce37c8a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -218,6 +218,18 @@ def mode(self, axis, numeric_only): | |||
""" | ||||
pass | ||||
|
||||
def good_import(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 | ||||
|
@@ -502,6 +514,33 @@ def no_punctuation(self): | |||
return "Hello world!" | ||||
|
||||
|
||||
class BadExamples(object): | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe there is already an example that imports both of these:
Any reason we don't leverage that instead of creating a new class? |
||||
|
||||
def numpy_import(self): | ||||
""" | ||||
Provide example with numpy import. | ||||
|
||||
Examples | ||||
-------- | ||||
This example does not import pandas. | ||||
>>> import numpy as np | ||||
>>> import datetime | ||||
""" | ||||
pass | ||||
|
||||
def pandas_import(self): | ||||
""" | ||||
Provide example with pandas import. | ||||
|
||||
Examples | ||||
-------- | ||||
This example does not import numpy. | ||||
>>> import pandas as pd | ||||
>>> import pickle | ||||
""" | ||||
pass | ||||
|
||||
|
||||
class TestValidator(object): | ||||
|
||||
def _import_path(self, klass=None, func=None): | ||||
|
@@ -600,7 +639,12 @@ 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 tests | ||||
('BadExamples', 'numpy_import', | ||||
('Examples should not have `import numpy` ',)), | ||||
('BadExamples', 'pandas_import', | ||||
('Examples should not have `import pandas` ',)) | ||||
]) | ||||
def test_bad_examples(self, capsys, klass, func, msgs): | ||||
result = validate_one(self._import_path(klass=klass, func=func)) # noqa:F821 | ||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -402,6 +402,12 @@ def examples_errors(self): | |||||
error_msgs += f.getvalue() | ||||||
return error_msgs | ||||||
|
||||||
@property | ||||||
def examples_codes(self): | ||||||
codes = doctest.DocTestParser().get_examples(self.raw_doc) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
codes = [line.source for line in codes] | ||||||
return ' '.join(codes) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you return a list with each line instead? We'll reuse this property to validate pep8 too, and returning a single string with all the code joined is not useful in that case. Also, at least to me |
||||||
|
||||||
|
||||||
def validate_one(func_name): | ||||||
""" | ||||||
|
@@ -512,6 +518,11 @@ def validate_one(func_name): | |||||
examples_errs = doc.examples_errors | ||||||
if examples_errs: | ||||||
errs.append('Examples do not pass tests') | ||||||
examples_codes = doc.examples_codes | ||||||
if 'import numpy' in examples_codes: | ||||||
errs.append('Examples should not have `import numpy` ') | ||||||
if 'import pandas' in examples_codes: | ||||||
errs.append('Examples should not have `import pandas` ') | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the error could be more descriptive. |
||||||
|
||||||
return {'type': doc.type, | ||||||
'docstring': doc.clean_doc, | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.