-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve --update-data handler (#15283)
- Loading branch information
Showing
6 changed files
with
274 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import shlex | ||
import subprocess | ||
import sys | ||
import textwrap | ||
from pathlib import Path | ||
|
||
from mypy.test.config import test_data_prefix | ||
from mypy.test.helpers import Suite | ||
|
||
|
||
class UpdateDataSuite(Suite): | ||
def _run_pytest_update_data(self, data_suite: str, *, max_attempts: int) -> str: | ||
""" | ||
Runs a suite of data test cases through 'pytest --update-data' until either tests pass | ||
or until a maximum number of attempts (needed for incremental tests). | ||
""" | ||
p = Path(test_data_prefix) / "check-update-data.test" | ||
assert not p.exists() | ||
try: | ||
p.write_text(textwrap.dedent(data_suite).lstrip()) | ||
|
||
test_nodeid = f"mypy/test/testcheck.py::TypeCheckSuite::{p.name}" | ||
args = [sys.executable, "-m", "pytest", "-n", "0", "-s", "--update-data", test_nodeid] | ||
if sys.version_info >= (3, 8): | ||
cmd = shlex.join(args) | ||
else: | ||
cmd = " ".join(args) | ||
for i in range(max_attempts - 1, -1, -1): | ||
res = subprocess.run(args) | ||
if res.returncode == 0: | ||
break | ||
print(f"`{cmd}` returned {res.returncode}: {i} attempts remaining") | ||
|
||
return p.read_text() | ||
finally: | ||
p.unlink() | ||
|
||
def test_update_data(self) -> None: | ||
# Note: We test multiple testcases rather than 'test case per test case' | ||
# so we could also exercise rewriting multiple testcases at once. | ||
actual = self._run_pytest_update_data( | ||
""" | ||
[case testCorrect] | ||
s: str = 42 # E: Incompatible types in assignment (expression has type "int", variable has type "str") | ||
[case testWrong] | ||
s: str = 42 # E: wrong error | ||
[case testWrongMultiline] | ||
s: str = 42 # E: foo \ | ||
# N: bar | ||
[case testMissingMultiline] | ||
s: str = 42; i: int = 'foo' | ||
[case testExtraneous] | ||
s: str = 'foo' # E: wrong error | ||
[case testExtraneousMultiline] | ||
s: str = 'foo' # E: foo \ | ||
# E: bar | ||
[case testExtraneousMultilineNonError] | ||
s: str = 'foo' # W: foo \ | ||
# N: bar | ||
[case testOutCorrect] | ||
s: str = 42 | ||
[out] | ||
main:1: error: Incompatible types in assignment (expression has type "int", variable has type "str") | ||
[case testOutWrong] | ||
s: str = 42 | ||
[out] | ||
main:1: error: foobar | ||
[case testOutWrongIncremental] | ||
s: str = 42 | ||
[out] | ||
main:1: error: foobar | ||
[out2] | ||
main:1: error: foobar | ||
[case testWrongMultipleFiles] | ||
import a, b | ||
s: str = 42 # E: foo | ||
[file a.py] | ||
s1: str = 42 # E: bar | ||
[file b.py] | ||
s2: str = 43 # E: baz | ||
[builtins fixtures/list.pyi] | ||
""", | ||
max_attempts=3, | ||
) | ||
|
||
# Assert | ||
expected = """ | ||
[case testCorrect] | ||
s: str = 42 # E: Incompatible types in assignment (expression has type "int", variable has type "str") | ||
[case testWrong] | ||
s: str = 42 # E: Incompatible types in assignment (expression has type "int", variable has type "str") | ||
[case testWrongMultiline] | ||
s: str = 42 # E: Incompatible types in assignment (expression has type "int", variable has type "str") | ||
[case testMissingMultiline] | ||
s: str = 42; i: int = 'foo' # E: Incompatible types in assignment (expression has type "int", variable has type "str") \\ | ||
# E: Incompatible types in assignment (expression has type "str", variable has type "int") | ||
[case testExtraneous] | ||
s: str = 'foo' | ||
[case testExtraneousMultiline] | ||
s: str = 'foo' | ||
[case testExtraneousMultilineNonError] | ||
s: str = 'foo' | ||
[case testOutCorrect] | ||
s: str = 42 | ||
[out] | ||
main:1: error: Incompatible types in assignment (expression has type "int", variable has type "str") | ||
[case testOutWrong] | ||
s: str = 42 | ||
[out] | ||
main:1: error: Incompatible types in assignment (expression has type "int", variable has type "str") | ||
[case testOutWrongIncremental] | ||
s: str = 42 | ||
[out] | ||
main:1: error: Incompatible types in assignment (expression has type "int", variable has type "str") | ||
[out2] | ||
main:1: error: Incompatible types in assignment (expression has type "int", variable has type "str") | ||
[case testWrongMultipleFiles] | ||
import a, b | ||
s: str = 42 # E: Incompatible types in assignment (expression has type "int", variable has type "str") | ||
[file a.py] | ||
s1: str = 42 # E: Incompatible types in assignment (expression has type "int", variable has type "str") | ||
[file b.py] | ||
s2: str = 43 # E: Incompatible types in assignment (expression has type "int", variable has type "str") | ||
[builtins fixtures/list.pyi] | ||
""" | ||
assert actual == textwrap.dedent(expected).lstrip() |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
from __future__ import annotations | ||
|
||
import re | ||
from collections import defaultdict | ||
from typing import Iterator | ||
|
||
from mypy.test.data import DataDrivenTestCase, DataFileCollector, DataFileFix, parse_test_data | ||
|
||
|
||
def update_testcase_output( | ||
testcase: DataDrivenTestCase, actual: list[str], *, incremental_step: int | ||
) -> None: | ||
collector = testcase.parent | ||
assert isinstance(collector, DataFileCollector) | ||
for fix in _iter_fixes(testcase, actual, incremental_step=incremental_step): | ||
collector.enqueue_fix(fix) | ||
|
||
|
||
def _iter_fixes( | ||
testcase: DataDrivenTestCase, actual: list[str], *, incremental_step: int | ||
) -> Iterator[DataFileFix]: | ||
reports_by_line: dict[tuple[str, int], list[tuple[str, str]]] = defaultdict(list) | ||
for error_line in actual: | ||
comment_match = re.match( | ||
r"^(?P<filename>[^:]+):(?P<lineno>\d+): (?P<severity>error|note|warning): (?P<msg>.+)$", | ||
error_line, | ||
) | ||
if comment_match: | ||
filename = comment_match.group("filename") | ||
lineno = int(comment_match.group("lineno")) | ||
severity = comment_match.group("severity") | ||
msg = comment_match.group("msg") | ||
reports_by_line[filename, lineno].append((severity, msg)) | ||
|
||
test_items = parse_test_data(testcase.data, testcase.name) | ||
|
||
# If we have [out] and/or [outN], we update just those sections. | ||
if any(re.match(r"^out\d*$", test_item.id) for test_item in test_items): | ||
for test_item in test_items: | ||
if (incremental_step < 2 and test_item.id == "out") or ( | ||
incremental_step >= 2 and test_item.id == f"out{incremental_step}" | ||
): | ||
yield DataFileFix( | ||
lineno=testcase.line + test_item.line - 1, | ||
end_lineno=testcase.line + test_item.end_line - 1, | ||
lines=actual + [""] * test_item.trimmed_newlines, | ||
) | ||
|
||
return | ||
|
||
# Update assertion comments within the sections | ||
for test_item in test_items: | ||
if test_item.id == "case": | ||
source_lines = test_item.data | ||
file_path = "main" | ||
elif test_item.id == "file": | ||
source_lines = test_item.data | ||
file_path = f"tmp/{test_item.arg}" | ||
else: | ||
continue # other sections we don't touch | ||
|
||
fix_lines = [] | ||
for lineno, source_line in enumerate(source_lines, start=1): | ||
reports = reports_by_line.get((file_path, lineno)) | ||
comment_match = re.search(r"(?P<indent>\s+)(?P<comment># [EWN]: .+)$", source_line) | ||
if comment_match: | ||
source_line = source_line[: comment_match.start("indent")] # strip old comment | ||
if reports: | ||
indent = comment_match.group("indent") if comment_match else " " | ||
# multiline comments are on the first line and then on subsequent lines emtpy lines | ||
# with a continuation backslash | ||
for j, (severity, msg) in enumerate(reports): | ||
out_l = source_line if j == 0 else " " * len(source_line) | ||
is_last = j == len(reports) - 1 | ||
severity_char = severity[0].upper() | ||
continuation = "" if is_last else " \\" | ||
fix_lines.append(f"{out_l}{indent}# {severity_char}: {msg}{continuation}") | ||
else: | ||
fix_lines.append(source_line) | ||
|
||
yield DataFileFix( | ||
lineno=testcase.line + test_item.line - 1, | ||
end_lineno=testcase.line + test_item.end_line - 1, | ||
lines=fix_lines + [""] * test_item.trimmed_newlines, | ||
) |