Skip to content

Commit 9bc7f65

Browse files
committed
Added test and slimmed PR
* added test * simplified temporary file usage * included flake8 by calling application directly * removed doctests from setup.cfg Signed-off-by: Fabian Haase <[email protected]>
1 parent 0358c21 commit 9bc7f65

File tree

3 files changed

+52
-48
lines changed

3 files changed

+52
-48
lines changed

scripts/tests/test_validate_docstrings.py

+28-2
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,29 @@ def prefix_pandas(self):
587587
pass
588588

589589

590+
class BadExamples(object):
591+
592+
def doctest(self):
593+
"""
594+
Return whether each value contains `pat`.
595+
596+
Examples
597+
--------
598+
>>> import pandas as pd
599+
>>> df = pd.DataFrame(np.ones((3, 3)),
600+
... columns=('a', 'b', 'c'))
601+
>>> df.all(1)
602+
0 True
603+
1 True
604+
2 True
605+
dtype: bool
606+
607+
>>> df.all(bool_only=True)
608+
Series([], dtype: bool)
609+
"""
610+
pass
611+
612+
590613
class TestValidator(object):
591614

592615
def _import_path(self, klass=None, func=None):
@@ -706,10 +729,13 @@ def test_bad_generic_functions(self, func):
706729
# See Also tests
707730
('BadSeeAlso', 'prefix_pandas',
708731
('pandas.Series.rename in `See Also` section '
709-
'does not need `pandas` prefix',))
732+
'does not need `pandas` prefix',)),
733+
# Examples tests
734+
('BadExamples', 'doctest',
735+
('1 F821 undefined name \'np\'',))
710736
])
711737
def test_bad_examples(self, capsys, klass, func, msgs):
712-
result = validate_one(self._import_path(klass=klass, func=func)) # noqa:F821
738+
result = validate_one(self._import_path(klass=klass, func=func))
713739
for msg in msgs:
714740
assert msg in ' '.join(result['errors'])
715741

scripts/validate_docstrings.py

+24-24
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
import inspect
2525
import importlib
2626
import doctest
27+
import tempfile
2728
from contextlib import contextmanager
2829

29-
from flake8.api import legacy as flake8
30+
from flake8.main.application import Application as Flake8
3031

3132
try:
3233
from io import StringIO
@@ -44,6 +45,7 @@
4445
from numpydoc.docscrape import NumpyDocString
4546
from pandas.io.formats.printing import pprint_thing
4647

48+
4749
PRIVATE_CLASSES = ['NDFrame', 'IndexOpsMixin']
4850
DIRECTIVES = ['versionadded', 'versionchanged', 'deprecated']
4951

@@ -336,40 +338,38 @@ def parameter_mismatches(self):
336338

337339
@property
338340
def pep8_violations(self):
339-
with self._file_representation() as filename:
340-
style_guide = flake8.get_style_guide(doctests=True)
341-
report = style_guide.input_file(filename=filename)
342-
return report.get_statistics('')
341+
with self._file_representation() as file:
342+
application = Flake8()
343+
application.initialize(["--doctests", "--quiet"])
344+
application.run_checks([file.name])
345+
application.report()
346+
stats = application.guide.stats
347+
return [
348+
"{} {} {}".format(s.count, s.error_code, s.message)
349+
for s in stats.statistics_for('')
350+
]
343351

344352
@contextmanager
345353
def _file_representation(self):
346354
"""
347355
Temporarily creates file with current function inside.
348356
The signature and body are **not** included.
349357
350-
:returns filename of tmp file
358+
:returns file
351359
"""
352360
create_function = 'def {name}():\n' \
353361
' """{doc}"""\n' \
354362
' pass\n'
355363

356-
tmp_dir = os.path.join(BASE_PATH, 'build', 'validate_docstring')
357-
os.makedirs(tmp_dir, exist_ok=True)
358-
359-
filename = os.path.join(tmp_dir, self.name + '.py')
360-
with open(filename, 'w') as f:
361-
name = self.name.split('.')[-1]
362-
lines = self.clean_doc.split("\n")
363-
indented_lines = [(' ' * 4) + line if line else ''
364-
for line in lines[1:]]
365-
doc = '\n'.join([lines[0], *indented_lines])
366-
367-
f.write(create_function.format(name=name, doc=doc))
368-
try:
369-
yield filename
370-
finally:
371-
os.remove(filename)
372-
os.rmdir(tmp_dir)
364+
name = self.name.split('.')[-1]
365+
lines = self.clean_doc.split("\n")
366+
indented_lines = [(' ' * 4) + line if line else ''
367+
for line in lines[1:]]
368+
doc = '\n'.join([lines[0], *indented_lines])
369+
with tempfile.NamedTemporaryFile(mode='w', suffix='.py') as file:
370+
file.write(create_function.format(name=name, doc=doc))
371+
file.flush()
372+
yield file
373373

374374
@property
375375
def correct_parameters(self):
@@ -532,7 +532,7 @@ def validate_one(func_name):
532532

533533
pep8_errs = doc.pep8_violations
534534
if pep8_errs:
535-
errs.append('Errors in doctest sections')
535+
errs.append('Errors in doctests')
536536
for pep8_err in pep8_errs:
537537
errs.append('\t{}'.format(pep8_err))
538538

setup.cfg

-22
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,6 @@ exclude =
2828
doc/temp/*.py,
2929
.eggs/*.py,
3030
versioneer.py
31-
.tox
32-
.git
33-
34-
doctests = True
35-
#TODO fix doctests
36-
exclude_from_doctest =
37-
./pandas/_libs
38-
./pandas/api
39-
./pandas/compat
40-
./pandas/core
41-
./pandas/errors
42-
./pandas/io
43-
./pandas/plotting
44-
./pandas/tests
45-
./pandas/tseries
46-
./pandas/util
47-
48-
[flake8-rst]
49-
ignore =
50-
F821, # undefined name
51-
W391, # blank line at end of file [Seems to be a bug (v0.4.1)]
52-
5331

5432
[yapf]
5533
based_on_style = pep8

0 commit comments

Comments
 (0)