Skip to content

Subprocess to validate flake8 #40812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions scripts/validate_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@
import importlib
import json
import os
import subprocess
import sys
import tempfile
from typing import (
List,
Optional,
)

import flake8.main.application

try:
from io import StringIO
except ImportError:
Expand Down Expand Up @@ -183,20 +182,21 @@ def validate_pep8(self):
)
)

application = flake8.main.application.Application()
application.initialize(["--quiet"])

error_messages = []
with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8") as file:
file.write(content)
file.flush()
application.run_checks([file.name])
cmd = ["python", "-m", "flake8", "--quiet", "--statistics", file.name]
response = subprocess.run(cmd, capture_output=True, text=True)
stdout = response.stdout
stdout = stdout.replace(file.name, "")
messages = stdout.strip("\n")
if messages:
error_messages.append(messages)

# We need this to avoid flake8 printing the names of the files to
# the standard output
application.formatter.write = lambda line, source: None
application.report()

yield from application.guide.stats.statistics_for("")
for error_message in error_messages:
error_count, error_code, message = error_message.split(maxsplit=2)
yield error_code, message, int(error_count)


def pandas_validate(func_name: str):
Expand Down Expand Up @@ -240,13 +240,15 @@ def pandas_validate(func_name: str):
result["errors"].append(
pandas_error("EX02", doctest_log=result["examples_errs"])
)
for err in doc.validate_pep8():

for error_code, error_message, error_count in doc.validate_pep8():
times_happening = f" ({error_count} times)" if error_count > 1 else ""
result["errors"].append(
pandas_error(
"EX03",
error_code=err.error_code,
error_message=err.message,
times_happening=f" ({err.count} times)" if err.count > 1 else "",
error_code=error_code,
error_message=error_message,
times_happening=times_happening,
)
)
examples_source_code = "".join(doc.examples_source_code)
Expand Down