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 4 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
44 changes: 27 additions & 17 deletions scripts/validate_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
import importlib
import json
import os
import re
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 +183,28 @@ 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])

# 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("")
# GH40784
cmd = ["flake8", "--quiet", "--statistics", file.name]
response = subprocess.run(cmd, capture_output=True, text=True)
stdout = response.stdout
# Remove file name from the start
stdout = stdout.replace(file.name, "")
# Remove the first and last elements since they are always empty str
messages = stdout.split("\n")[1:-1]
error_messages.extend(messages)

# Parse error message
for error_message in error_messages:
# Preserve whitespaces with a group
error_items = re.split(r"(\s+)", error_message)
error_count = int(error_items[0])
error_code = error_items[2]
message = "".join(error_items[4:])
yield error_code, message, error_count


def pandas_validate(func_name: str):
Expand Down Expand Up @@ -240,13 +248,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