Skip to content

Commit

Permalink
Use Python's builtin warnings filters
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood committed Jun 27, 2023
1 parent f341194 commit 1f3e353
Showing 1 changed file with 21 additions and 49 deletions.
70 changes: 21 additions & 49 deletions tests/test_pyi_files.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,12 @@
import glob
import os
import os.path
import re
import subprocess
import sys
from itertools import zip_longest

import pytest

IGNORED_DEPRECATION_WARNINGS = [
# Ignore all DeprecationWarnings that come from pyflakes or flake8-bugbear
rf"{re.escape(os.path.join('pyflakes', 'checker.py'))}:\d+: DeprecationWarning: ",
r"bugbear\.py:\d+: DeprecationWarning: ",
]
IGNORED_DEPRECATION_PATTERNS = [
re.compile(pattern) for pattern in IGNORED_DEPRECATION_WARNINGS
]


def get_filtered_stderr(stderr: str) -> str:
lines = stderr.splitlines()

grouped_lines = []
skip_this_line = False
for line, next_line in zip_longest(lines, lines[1:]):
if skip_this_line:
skip_this_line = False
elif next_line is None:
grouped_lines.append(line)
elif "DeprecationWarning" in line:
grouped_lines.append(line + "\n" + next_line)
skip_this_line = True
else:
grouped_lines.append(line)

return "\n".join(
line
for line in grouped_lines
if not any(pattern.search(line) for pattern in IGNORED_DEPRECATION_PATTERNS)
)


@pytest.mark.parametrize("path", glob.glob("tests/*.pyi"))
def test_pyi_file(path: str) -> None:
Expand Down Expand Up @@ -75,27 +42,33 @@ def test_pyi_file(path: str) -> None:
option = flag.split("=")[0]
assert option != "--ignore", bad_flag_msg

# Silence DeprecationWarnings from our dependencies (pyflakes, flake8-bugbear, etc.)
#
# For DeprecationWarnings coming from flake8-pyi itself,
# print the first occurence of each warning to stderr.
# This will fail CI the same as `-Werror:::pyi`,
# but the test failure report that pyflakes gives is much easier to read
# if we use `-Wdefault:::pyi`
flake8_invocation = [
sys.executable,
"-Wignore",
"-Wdefault:::pyi",
"-m",
"flake8",
"-j0",
]

run_results = [
# Passing a file on command line
subprocess.run(
[sys.executable, "-Walways", "-m", "flake8", "-j0", *flags, path],
[*flake8_invocation, *flags, path],
env={**os.environ, "PYTHONPATH": "."},
capture_output=True,
text=True,
),
# Passing "-" as the file, and reading from stdin instead
subprocess.run(
[
sys.executable,
"-Walways",
"-m",
"flake8",
"-j0",
"--stdin-display-name",
path,
*flags,
"-",
],
[*flake8_invocation, "--stdin-display-name", path, *flags, "-"],
env={**os.environ, "PYTHONPATH": "."},
input=file_contents,
capture_output=True,
Expand All @@ -104,8 +77,7 @@ def test_pyi_file(path: str) -> None:
]

for run_result in run_results:
output = run_result.stdout
if stderr := get_filtered_stderr(run_result.stderr):
output += "\n" + stderr
output = re.sub(":[0-9]+: ", ": ", output) # ignore column numbers
output = re.sub(":[0-9]+: ", ": ", run_result.stdout) # ignore column numbers
if run_result.stderr:
output += "\n" + run_result.stderr
assert output == expected_output

0 comments on commit 1f3e353

Please sign in to comment.