Skip to content

Commit bc009a4

Browse files
committed
[skip ci] Run flake8 on tmpfile containing the docstring
Signed-off-by: Fabian Haase <[email protected]>
1 parent c87360d commit bc009a4

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

scripts/validate_docstrings.py

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

2829
from flake8.api import legacy as flake8
2930

@@ -335,9 +336,40 @@ def parameter_mismatches(self):
335336

336337
@property
337338
def pep8_violations(self):
338-
style_guide = flake8.get_style_guide(doctests=True)
339-
report = style_guide.input_file(filename=self.source_file_name)
340-
return report.get_statistics('')
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('')
343+
344+
@contextmanager
345+
def _file_representation(self):
346+
"""
347+
Creates a tmp file containing the function without the body
348+
349+
:returns filename of tmp file
350+
"""
351+
create_function = 'def {name}{signature}: # noqa: E501\n' \
352+
' """{doc}"""\n' \
353+
' pass\n'
354+
355+
tmp_dir = os.path.join(BASE_PATH, 'build', 'validate_docstring')
356+
os.makedirs(tmp_dir, exist_ok=True)
357+
358+
filename = os.path.join(tmp_dir, self.name + '.py')
359+
with open(filename, 'w') as f:
360+
name = self.name.split('.')[-1]
361+
sig = str(inspect.signature(self.obj))
362+
lines = self.raw_doc.split("\n")
363+
indented_lines = [' ' + 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, signature=sig, doc=doc))
368+
369+
yield filename
370+
371+
os.remove(filename)
372+
os.rmdir(tmp_dir)
341373

342374
@property
343375
def correct_parameters(self):

0 commit comments

Comments
 (0)