|
24 | 24 | import inspect |
25 | 25 | import importlib |
26 | 26 | import doctest |
| 27 | +import tempfile |
27 | 28 | from contextlib import contextmanager |
28 | 29 |
|
29 | | -from flake8.api import legacy as flake8 |
| 30 | +from flake8.main.application import Application as Flake8 |
30 | 31 |
|
31 | 32 | try: |
32 | 33 | from io import StringIO |
|
44 | 45 | from numpydoc.docscrape import NumpyDocString |
45 | 46 | from pandas.io.formats.printing import pprint_thing |
46 | 47 |
|
| 48 | + |
47 | 49 | PRIVATE_CLASSES = ['NDFrame', 'IndexOpsMixin'] |
48 | 50 | DIRECTIVES = ['versionadded', 'versionchanged', 'deprecated'] |
49 | 51 |
|
@@ -336,40 +338,38 @@ def parameter_mismatches(self): |
336 | 338 |
|
337 | 339 | @property |
338 | 340 | 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"]) |
| 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 | + ] |
343 | 351 |
|
344 | 352 | @contextmanager |
345 | 353 | def _file_representation(self): |
346 | 354 | """ |
347 | 355 | Temporarily creates file with current function inside. |
348 | 356 | The signature and body are **not** included. |
349 | 357 |
|
350 | | - :returns filename of tmp file |
| 358 | + :returns file |
351 | 359 | """ |
352 | 360 | create_function = 'def {name}():\n' \ |
353 | 361 | ' """{doc}"""\n' \ |
354 | 362 | ' pass\n' |
355 | 363 |
|
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 |
373 | 373 |
|
374 | 374 | @property |
375 | 375 | def correct_parameters(self): |
@@ -532,7 +532,7 @@ def validate_one(func_name): |
532 | 532 |
|
533 | 533 | pep8_errs = doc.pep8_violations |
534 | 534 | if pep8_errs: |
535 | | - errs.append('Errors in doctest sections') |
| 535 | + errs.append('Errors in doctests') |
536 | 536 | for pep8_err in pep8_errs: |
537 | 537 | errs.append('\t{}'.format(pep8_err)) |
538 | 538 |
|
|
0 commit comments