Skip to content

Commit 5c071ec

Browse files
committed
Made changes as recommended
1 parent 8655fdd commit 5c071ec

File tree

2 files changed

+45
-14
lines changed

2 files changed

+45
-14
lines changed

scripts/tests/test_validate_docstrings.py

+34-8
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,18 @@ def mode(self, axis, numeric_only):
218218
"""
219219
pass
220220

221+
def good_import(self):
222+
"""
223+
Ensure import other than numpy and pandas are fine.
224+
225+
Examples
226+
--------
227+
This example does not import pandas or import numpy.
228+
>>> import time
229+
>>> import datetime
230+
"""
231+
pass
232+
221233

222234
class BadGenericDocStrings(object):
223235
"""Everything here has a bad docstring
@@ -504,14 +516,27 @@ def no_punctuation(self):
504516

505517
class BadExamples(object):
506518

507-
def npPd_import(self):
519+
def numpy_import(self):
520+
"""
521+
Provide example with numpy import.
522+
523+
Examples
524+
--------
525+
This example does not import pandas.
526+
>>> import numpy as np
527+
>>> import datetime
528+
"""
529+
pass
530+
531+
def pandas_import(self):
508532
"""
509-
Provide example with numpy and pandas import
533+
Provide example with pandas import.
510534
511535
Examples
512536
--------
513-
import numpy as np
514-
import pandas as pd
537+
This example does not import numpy.
538+
>>> import pandas as pd
539+
>>> import pickle
515540
"""
516541
pass
517542

@@ -615,10 +640,11 @@ def test_bad_generic_functions(self, func):
615640
marks=pytest.mark.xfail),
616641
pytest.param('BadReturns', 'no_punctuation', ('foo',),
617642
marks=pytest.mark.xfail),
618-
# Examples
619-
('BadExamples', 'npPd_import',
620-
('Examples should not have `import pandas as pd` ',
621-
'Examples should not have `import numpy as np` ',))
643+
# Examples tests
644+
('BadExamples', 'numpy_import',
645+
('Examples should not have `import numpy` ',)),
646+
('BadExamples', 'pandas_import',
647+
('Examples should not have `import pandas` ',))
622648
])
623649
def test_bad_examples(self, capsys, klass, func, msgs):
624650
result = validate_one(self._import_path(klass=klass, func=func)) # noqa:F821

scripts/validate_docstrings.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,12 @@ def examples_errors(self):
402402
error_msgs += f.getvalue()
403403
return error_msgs
404404

405+
@property
406+
def examples_codes(self):
407+
codes = doctest.DocTestParser().get_examples(self.raw_doc)
408+
codes = [line.source.rstrip() for line in codes]
409+
return ' '.join(codes)
410+
405411

406412
def validate_one(func_name):
407413
"""
@@ -512,12 +518,11 @@ def validate_one(func_name):
512518
examples_errs = doc.examples_errors
513519
if examples_errs:
514520
errs.append('Examples do not pass tests')
515-
if 'import numpy as np' in ' '.join(doc.examples):
516-
errs.append(' Examples should not have '
517-
'`import numpy as np` ')
518-
if 'import pandas as pd' in ' '.join(doc.examples):
519-
errs.append(' Examples should not have '
520-
'`import pandas as pd` ')
521+
examples_codes = doc.examples_codes
522+
if 'import numpy' in examples_codes:
523+
errs.append('Examples should not have `import numpy` ')
524+
if 'import pandas' in examples_codes:
525+
errs.append('Examples should not have `import pandas` ')
521526

522527
return {'type': doc.type,
523528
'docstring': doc.clean_doc,

0 commit comments

Comments
 (0)