Skip to content

Commit 46cd0a4

Browse files
committed
Added test, simplified temporary file usage
Signed-off-by: Fabian Haase <[email protected]>
1 parent 0358c21 commit 46cd0a4

File tree

3 files changed

+43
-30
lines changed

3 files changed

+43
-30
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

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

2930
from flake8.api import legacy as flake8
@@ -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,9 +338,9 @@ def parameter_mismatches(self):
336338

337339
@property
338340
def pep8_violations(self):
339-
with self._file_representation() as filename:
341+
with self._file_representation() as file:
340342
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)
342344
return report.get_statistics('')
343345

344346
@contextmanager
@@ -347,29 +349,21 @@ def _file_representation(self):
347349
Temporarily creates file with current function inside.
348350
The signature and body are **not** included.
349351
350-
:returns filename of tmp file
352+
:returns file
351353
"""
352354
create_function = 'def {name}():\n' \
353355
' """{doc}"""\n' \
354356
' pass\n'
355357

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
373367

374368
@property
375369
def correct_parameters(self):
@@ -534,6 +528,7 @@ def validate_one(func_name):
534528
if pep8_errs:
535529
errs.append('Errors in doctest sections')
536530
for pep8_err in pep8_errs:
531+
print(pep8_err)
537532
errs.append('\t{}'.format(pep8_err))
538533

539534
if doc.is_function_or_method:

setup.cfg

-8
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ exclude =
2828
doc/temp/*.py,
2929
.eggs/*.py,
3030
versioneer.py
31-
.tox
32-
.git
3331

3432
doctests = True
3533
#TODO fix doctests
@@ -45,12 +43,6 @@ exclude_from_doctest =
4543
./pandas/tseries
4644
./pandas/util
4745

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-
53-
5446
[yapf]
5547
based_on_style = pep8
5648
split_before_named_assigns = false

0 commit comments

Comments
 (0)