@@ -225,8 +225,9 @@ def good_imports(self):
225
225
Examples
226
226
--------
227
227
This example does not import pandas or import numpy.
228
- >>> import time
229
228
>>> import datetime
229
+ >>> datetime.MAXYEAR
230
+ 9999
230
231
"""
231
232
pass
232
233
@@ -596,6 +597,44 @@ def prefix_pandas(self):
596
597
pass
597
598
598
599
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
+
599
638
class TestValidator(object):
600
639
601
640
def _import_path(self, klass=None, func=None):
@@ -634,7 +673,7 @@ def test_good_class(self):
634
673
@capture_stderr
635
674
@pytest.mark.parametrize("func", [
636
675
'plot', 'sample', 'random_letters', 'sample_values', 'head', 'head1',
637
- 'contains', 'mode'])
676
+ 'contains', 'mode', 'good_imports' ])
638
677
def test_good_functions(self, func):
639
678
errors = validate_one(self._import_path(
640
679
klass='GoodDocStrings', func=func))['errors']
@@ -714,16 +753,25 @@ def test_bad_generic_functions(self, func):
714
753
marks=pytest.mark.xfail),
715
754
# Examples tests
716
755
('BadGenericDocStrings', 'method',
717
- ('numpy does not need to be imported in the examples,' )),
756
+ ('numpy does not need to be imported in the examples', )),
718
757
('BadGenericDocStrings', 'method',
719
- ('pandas does not need to be imported in the examples,' )),
758
+ ('pandas does not need to be imported in the examples', )),
720
759
# See Also tests
721
760
('BadSeeAlso', 'prefix_pandas',
722
761
('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 \',\'',)),
724
772
])
725
773
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))
727
775
for msg in msgs:
728
776
assert msg in ' '.join(result['errors'])
729
777
0 commit comments