24
24
import inspect
25
25
import importlib
26
26
import doctest
27
+ import tempfile
27
28
from contextlib import contextmanager
28
29
29
30
from flake8 .api import legacy as flake8
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,9 +338,9 @@ def parameter_mismatches(self):
336
338
337
339
@property
338
340
def pep8_violations (self ):
339
- with self ._file_representation () as filename :
341
+ with self ._file_representation () as file :
340
342
style_guide = flake8 .get_style_guide (doctests = True )
341
- report = style_guide .input_file (filename = filename )
343
+ report = style_guide .input_file (filename = file . name )
342
344
return report .get_statistics ('' )
343
345
344
346
@contextmanager
@@ -347,29 +349,21 @@ def _file_representation(self):
347
349
Temporarily creates file with current function inside.
348
350
The signature and body are **not** included.
349
351
350
- :returns filename of tmp file
352
+ :returns file
351
353
"""
352
354
create_function = 'def {name}():\n ' \
353
355
' """{doc}"""\n ' \
354
356
' pass\n '
355
357
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 )
358
+ name = self .name .split ('.' )[- 1 ]
359
+ lines = self .clean_doc .split ("\n " )
360
+ indented_lines = [(' ' * 4 ) + line if line else ''
361
+ for line in lines [1 :]]
362
+ doc = '\n ' .join ([lines [0 ], * indented_lines ])
363
+ with tempfile .NamedTemporaryFile (mode = 'w' , suffix = '.py' ) as file :
364
+ file .write (create_function .format (name = name , doc = doc ))
365
+ file .flush ()
366
+ yield file
373
367
374
368
@property
375
369
def correct_parameters (self ):
@@ -534,6 +528,7 @@ def validate_one(func_name):
534
528
if pep8_errs :
535
529
errs .append ('Errors in doctest sections' )
536
530
for pep8_err in pep8_errs :
531
+ print (pep8_err )
537
532
errs .append ('\t {}' .format (pep8_err ))
538
533
539
534
if doc .is_function_or_method :
0 commit comments