|
24 | 24 | import inspect
|
25 | 25 | import importlib
|
26 | 26 | import doctest
|
| 27 | +from contextlib import contextmanager |
27 | 28 |
|
28 | 29 | from flake8.api import legacy as flake8
|
29 | 30 |
|
@@ -335,9 +336,40 @@ def parameter_mismatches(self):
|
335 | 336 |
|
336 | 337 | @property
|
337 | 338 | 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) |
341 | 373 |
|
342 | 374 | @property
|
343 | 375 | def correct_parameters(self):
|
|
0 commit comments