Skip to content

Commit 047242b

Browse files
thooTomAugspurger
authored andcommitted
DOC: Validate in docstrings that numpy and pandas are not imported (#23161)
1 parent 07426af commit 047242b

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

scripts/tests/test_validate_docstrings.py

+17
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_imports(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
@@ -700,6 +712,11 @@ def test_bad_generic_functions(self, func):
700712
marks=pytest.mark.xfail),
701713
pytest.param('BadReturns', 'no_punctuation', ('foo',),
702714
marks=pytest.mark.xfail),
715+
# Examples tests
716+
('BadGenericDocStrings', 'method',
717+
('numpy does not need to be imported in the examples,')),
718+
('BadGenericDocStrings', 'method',
719+
('pandas does not need to be imported in the examples,')),
703720
# See Also tests
704721
('BadSeeAlso', 'prefix_pandas',
705722
('pandas.Series.rename in `See Also` section '

scripts/validate_docstrings.py

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

405+
@property
406+
def examples_source_code(self):
407+
lines = doctest.DocTestParser().get_examples(self.raw_doc)
408+
return [line.source for line in lines]
409+
405410

406411
def validate_one(func_name):
407412
"""
@@ -531,6 +536,13 @@ def validate_one(func_name):
531536
examples_errs = doc.examples_errors
532537
if examples_errs:
533538
errs.append('Examples do not pass tests')
539+
examples_source_code = ''.join(doc.examples_source_code)
540+
if 'import numpy' in examples_source_code:
541+
errs.append("numpy does not need to be imported in the examples, "
542+
"as it's assumed to be already imported as np")
543+
if 'import pandas' in examples_source_code:
544+
errs.append("pandas does not need to be imported in the examples, "
545+
"as it's assumed to be already imported as pd")
534546

535547
return {'type': doc.type,
536548
'docstring': doc.clean_doc,

0 commit comments

Comments
 (0)