@@ -225,8 +225,9 @@ def good_imports(self):
225225 Examples
226226 --------
227227 This example does not import pandas or import numpy.
228- >>> import time
229228 >>> import datetime
229+ >>> datetime.MAXYEAR
230+ 9999
230231 """
231232 pass
232233
@@ -596,6 +597,44 @@ def prefix_pandas(self):
596597 pass
597598
598599
600+ class BadExamples (object ):
601+
602+ def unused_import (self ):
603+ """
604+ Examples
605+ --------
606+ >>> import pandas as pdf
607+ >>> df = pd.DataFrame(np.ones((3, 3)), columns=('a', 'b', 'c'))
608+ """
609+ pass
610+
611+ def missing_whitespace_around_arithmetic_operator (self ):
612+ """
613+ Examples
614+ --------
615+ >>> 2+5
616+ 7
617+ """
618+ pass
619+
620+ def indentation_is_not_a_multiple_of_four (self ):
621+ """
622+ Examples
623+ --------
624+ >>> if 2 + 5:
625+ ... pass
626+ """
627+ pass
628+
629+ def missing_whitespace_after_comma (self ):
630+ """
631+ Examples
632+ --------
633+ >>> df = pd.DataFrame(np.ones((3,3)),columns=('a','b', 'c'))
634+ """
635+ pass
636+
637+
599638class TestValidator (object ):
600639
601640 def _import_path (self , klass = None , func = None ):
@@ -634,7 +673,7 @@ def test_good_class(self):
634673 @capture_stderr
635674 @pytest .mark .parametrize ("func" , [
636675 'plot' , 'sample' , 'random_letters' , 'sample_values' , 'head' , 'head1' ,
637- 'contains' , 'mode' ])
676+ 'contains' , 'mode' , 'good_imports' ])
638677 def test_good_functions (self , func ):
639678 errors = validate_one (self ._import_path (
640679 klass = 'GoodDocStrings' , func = func ))['errors' ]
@@ -714,16 +753,25 @@ def test_bad_generic_functions(self, func):
714753 marks = pytest .mark .xfail ),
715754 # Examples tests
716755 ('BadGenericDocStrings' , 'method' ,
717- ('numpy does not need to be imported in the examples,' )),
756+ ('numpy does not need to be imported in the examples' , )),
718757 ('BadGenericDocStrings' , 'method' ,
719- ('pandas does not need to be imported in the examples,' )),
758+ ('pandas does not need to be imported in the examples' , )),
720759 # See Also tests
721760 ('BadSeeAlso' , 'prefix_pandas' ,
722761 ('pandas.Series.rename in `See Also` section '
723- 'does not need `pandas` prefix' ,))
762+ 'does not need `pandas` prefix' ,)),
763+ # Examples tests
764+ ('BadExamples' , 'unused_import' ,
765+ ('1 F401 \' pandas as pdf\' imported but unused' ,)),
766+ ('BadExamples' , 'indentation_is_not_a_multiple_of_four' ,
767+ ('1 E111 indentation is not a multiple of four' ,)),
768+ ('BadExamples' , 'missing_whitespace_around_arithmetic_operator' ,
769+ ('1 E226 missing whitespace around arithmetic operator' ,)),
770+ ('BadExamples' , 'missing_whitespace_after_comma' ,
771+ ('3 E231 missing whitespace after \' ,\' ' ,)),
724772 ])
725773 def test_bad_examples (self , capsys , klass , func , msgs ):
726- result = validate_one (self ._import_path (klass = klass , func = func )) # noqa:F821
774+ result = validate_one (self ._import_path (klass = klass , func = func ))
727775 for msg in msgs :
728776 assert msg in ' ' .join (result ['errors' ])
729777
0 commit comments