Skip to content

Commit 823d84c

Browse files
hasan-yamanJulianWgs
hasan-yaman
authored andcommitted
Subprocess to validate flake8 (pandas-dev#40812)
1 parent cb57533 commit 823d84c

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

scripts/validate_docstrings.py

+18-16
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@
1919
import importlib
2020
import json
2121
import os
22+
import subprocess
2223
import sys
2324
import tempfile
2425
from typing import (
2526
List,
2627
Optional,
2728
)
2829

29-
import flake8.main.application
30-
3130
try:
3231
from io import StringIO
3332
except ImportError:
@@ -183,20 +182,21 @@ def validate_pep8(self):
183182
)
184183
)
185184

186-
application = flake8.main.application.Application()
187-
application.initialize(["--quiet"])
188-
185+
error_messages = []
189186
with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8") as file:
190187
file.write(content)
191188
file.flush()
192-
application.run_checks([file.name])
189+
cmd = ["python", "-m", "flake8", "--quiet", "--statistics", file.name]
190+
response = subprocess.run(cmd, capture_output=True, text=True)
191+
stdout = response.stdout
192+
stdout = stdout.replace(file.name, "")
193+
messages = stdout.strip("\n")
194+
if messages:
195+
error_messages.append(messages)
193196

194-
# We need this to avoid flake8 printing the names of the files to
195-
# the standard output
196-
application.formatter.write = lambda line, source: None
197-
application.report()
198-
199-
yield from application.guide.stats.statistics_for("")
197+
for error_message in error_messages:
198+
error_count, error_code, message = error_message.split(maxsplit=2)
199+
yield error_code, message, int(error_count)
200200

201201

202202
def pandas_validate(func_name: str):
@@ -240,13 +240,15 @@ def pandas_validate(func_name: str):
240240
result["errors"].append(
241241
pandas_error("EX02", doctest_log=result["examples_errs"])
242242
)
243-
for err in doc.validate_pep8():
243+
244+
for error_code, error_message, error_count in doc.validate_pep8():
245+
times_happening = f" ({error_count} times)" if error_count > 1 else ""
244246
result["errors"].append(
245247
pandas_error(
246248
"EX03",
247-
error_code=err.error_code,
248-
error_message=err.message,
249-
times_happening=f" ({err.count} times)" if err.count > 1 else "",
249+
error_code=error_code,
250+
error_message=error_message,
251+
times_happening=times_happening,
250252
)
251253
)
252254
examples_source_code = "".join(doc.examples_source_code)

0 commit comments

Comments
 (0)