Skip to content

Commit

Permalink
Added test and slimmed PR
Browse files Browse the repository at this point in the history
* added test
* simplified temporary file usage
* included flake8 by calling application directly
* removed doctests from setup.cfg

Signed-off-by: Fabian Haase <haase.fabian@gmail.com>
  • Loading branch information
FHaase committed Nov 2, 2018
1 parent 0358c21 commit 9c09bf1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 48 deletions.
30 changes: 28 additions & 2 deletions scripts/tests/test_validate_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,29 @@ def prefix_pandas(self):
pass


class BadExamples(object):

def doctest(self):
"""
Return whether each value contains `pat`.
Examples
--------
>>> import pandas as pd
>>> df = pd.DataFrame(np.ones((3, 3)),
... columns=('a', 'b', 'c'))
>>> df.all(1)
0 True
1 True
2 True
dtype: bool
>>> df.all(bool_only=True)
Series([], dtype: bool)
"""
pass


class TestValidator(object):

def _import_path(self, klass=None, func=None):
Expand Down Expand Up @@ -706,10 +729,13 @@ def test_bad_generic_functions(self, func):
# See Also tests
('BadSeeAlso', 'prefix_pandas',
('pandas.Series.rename in `See Also` section '
'does not need `pandas` prefix',))
'does not need `pandas` prefix',)),
# Examples tests
('BadExamples', 'doctest',
('1 F821 undefined name \'np\'',))
])
def test_bad_examples(self, capsys, klass, func, msgs):
result = validate_one(self._import_path(klass=klass, func=func)) # noqa:F821
result = validate_one(self._import_path(klass=klass, func=func))
for msg in msgs:
assert msg in ' '.join(result['errors'])

Expand Down
48 changes: 24 additions & 24 deletions scripts/validate_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
import inspect
import importlib
import doctest
import tempfile
from contextlib import contextmanager

from flake8.api import legacy as flake8
from flake8.main.application import Application as Flake8

try:
from io import StringIO
Expand All @@ -44,6 +45,7 @@
from numpydoc.docscrape import NumpyDocString
from pandas.io.formats.printing import pprint_thing


PRIVATE_CLASSES = ['NDFrame', 'IndexOpsMixin']
DIRECTIVES = ['versionadded', 'versionchanged', 'deprecated']

Expand Down Expand Up @@ -336,40 +338,38 @@ def parameter_mismatches(self):

@property
def pep8_violations(self):
with self._file_representation() as filename:
style_guide = flake8.get_style_guide(doctests=True)
report = style_guide.input_file(filename=filename)
return report.get_statistics('')
with self._file_representation() as file:
application = Flake8()
application.initialize(["--doctests"])
application.run_checks([file.name])
application.report()
stats = application.guide.stats
return [
"{} {} {}".format(s.count, s.error_code, s.message)
for s in stats.statistics_for('')
]

@contextmanager
def _file_representation(self):
"""
Temporarily creates file with current function inside.
The signature and body are **not** included.
:returns filename of tmp file
:returns file
"""
create_function = 'def {name}():\n' \
' """{doc}"""\n' \
' pass\n'

tmp_dir = os.path.join(BASE_PATH, 'build', 'validate_docstring')
os.makedirs(tmp_dir, exist_ok=True)

filename = os.path.join(tmp_dir, self.name + '.py')
with open(filename, 'w') as f:
name = self.name.split('.')[-1]
lines = self.clean_doc.split("\n")
indented_lines = [(' ' * 4) + line if line else ''
for line in lines[1:]]
doc = '\n'.join([lines[0], *indented_lines])

f.write(create_function.format(name=name, doc=doc))
try:
yield filename
finally:
os.remove(filename)
os.rmdir(tmp_dir)
name = self.name.split('.')[-1]
lines = self.clean_doc.split("\n")
indented_lines = [(' ' * 4) + line if line else ''
for line in lines[1:]]
doc = '\n'.join([lines[0], *indented_lines])
with tempfile.NamedTemporaryFile(mode='w', suffix='.py') as file:
file.write(create_function.format(name=name, doc=doc))
file.flush()
yield file

@property
def correct_parameters(self):
Expand Down Expand Up @@ -532,7 +532,7 @@ def validate_one(func_name):

pep8_errs = doc.pep8_violations
if pep8_errs:
errs.append('Errors in doctest sections')
errs.append('Errors in doctests')
for pep8_err in pep8_errs:
errs.append('\t{}'.format(pep8_err))

Expand Down
22 changes: 0 additions & 22 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,6 @@ exclude =
doc/temp/*.py,
.eggs/*.py,
versioneer.py
.tox
.git

doctests = True
#TODO fix doctests
exclude_from_doctest =
./pandas/_libs
./pandas/api
./pandas/compat
./pandas/core
./pandas/errors
./pandas/io
./pandas/plotting
./pandas/tests
./pandas/tseries
./pandas/util

[flake8-rst]
ignore =
F821, # undefined name
W391, # blank line at end of file [Seems to be a bug (v0.4.1)]


[yapf]
based_on_style = pep8
Expand Down

0 comments on commit 9c09bf1

Please sign in to comment.