MCPcopy Index your code
hub / github.com/python/mypy / test_output_json

Function test_output_json

mypy/test/testoutput.py:26–64  ·  view source on GitHub ↗

Runs Mypy in a subprocess, and ensures that `--output=json` works as intended.

(testcase: DataDrivenTestCase)

Source from the content-addressed store, hash-verified

24
25
26def test_output_json(testcase: DataDrivenTestCase) -> None:
27 """Runs Mypy in a subprocess, and ensures that `--output=json` works as intended."""
28 program_text = "\n".join(testcase.input)
29 flags_match = re.search("# flags: (.*)$", program_text, flags=re.MULTILINE)
30 if flags_match is not None:
31 mypy_cmdline = flags_match.group(1).split()
32 else:
33 mypy_cmdline = ["--output=json"]
34 mypy_cmdline.append(f"--python-version={'.'.join(map(str, PYTHON3_VERSION))}")
35
36 # Write the program to a file.
37 program_path = os.path.join(test_temp_dir, "main")
38 mypy_cmdline.append(program_path)
39 with open(program_path, "w", encoding="utf8") as file:
40 for s in testcase.input:
41 file.write(f"{s}\n")
42
43 output = []
44 # Type check the program.
45 out, err, returncode = api.run(mypy_cmdline)
46 # split lines, remove newlines, and remove directory of test case
47 for line in (out + err).rstrip("\n").splitlines():
48 if line.startswith(test_temp_dir + os.sep):
49 output.append(line[len(test_temp_dir + os.sep) :].rstrip("\r\n"))
50 else:
51 output.append(line.rstrip("\r\n"))
52
53 if returncode > 1:
54 output.append("!!! Mypy crashed !!!")
55
56 # Remove temp file.
57 os.remove(program_path)
58
59 # JSON encodes every `\` character into `\\`, so we need to remove `\\` from windows paths
60 # and `/` from POSIX paths
61 json_os_separator = os.sep.replace("\\", "\\\\")
62 normalized_output = [line.replace(test_temp_dir + json_os_separator, "") for line in output]
63
64 assert normalized_output == testcase.output

Callers 1

run_caseMethod · 0.85

Calls 13

mapClass · 0.85
lenFunction · 0.85
splitMethod · 0.80
groupMethod · 0.80
appendMethod · 0.80
splitlinesMethod · 0.80
rstripMethod · 0.80
replaceMethod · 0.80
joinMethod · 0.45
writeMethod · 0.45
runMethod · 0.45
startswithMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…