Test running mypy on files that depend on PEP 561 packages.
(testcase: DataDrivenTestCase)
| 89 | |
| 90 | |
| 91 | def test_pep561(testcase: DataDrivenTestCase) -> None: |
| 92 | """Test running mypy on files that depend on PEP 561 packages.""" |
| 93 | assert testcase.old_cwd is not None, "test was not properly set up" |
| 94 | python = sys.executable |
| 95 | |
| 96 | assert python is not None, "Should be impossible" |
| 97 | pkgs, pip_args = parse_pkgs(testcase.input[0]) |
| 98 | mypy_args = parse_mypy_args(testcase.input[1]) |
| 99 | editable = False |
| 100 | for arg in pip_args: |
| 101 | if arg == "editable": |
| 102 | editable = True |
| 103 | else: |
| 104 | raise ValueError(f"Unknown pip argument: {arg}") |
| 105 | assert pkgs, "No packages to install for PEP 561 test?" |
| 106 | with virtualenv(python) as venv: |
| 107 | venv_dir, python_executable = venv |
| 108 | if editable: |
| 109 | # Editable installs with PEP 660 require pip>=21.3 |
| 110 | upgrade_pip(python_executable) |
| 111 | for pkg in pkgs: |
| 112 | install_package(pkg, python_executable, editable) |
| 113 | |
| 114 | cmd_line = list(mypy_args) |
| 115 | has_program = not ("-p" in cmd_line or "--package" in cmd_line) |
| 116 | if has_program: |
| 117 | program = testcase.name + ".py" |
| 118 | with open(program, "w", encoding="utf-8") as f: |
| 119 | for s in testcase.input: |
| 120 | f.write(f"{s}\n") |
| 121 | cmd_line.append(program) |
| 122 | |
| 123 | cmd_line.extend(["--no-error-summary", "--hide-error-codes"]) |
| 124 | if python_executable != sys.executable: |
| 125 | cmd_line.append(f"--python-executable={python_executable}") |
| 126 | |
| 127 | steps = testcase.find_steps() |
| 128 | if steps != [[]]: |
| 129 | steps = [[]] + steps |
| 130 | |
| 131 | for i, operations in enumerate(steps): |
| 132 | perform_file_operations(operations) |
| 133 | |
| 134 | output = [] |
| 135 | # Type check the module |
| 136 | out, err, returncode = mypy.api.run(cmd_line) |
| 137 | |
| 138 | # split lines, remove newlines, and remove directory of test case |
| 139 | for line in (out + err).splitlines(): |
| 140 | if line.startswith(test_temp_dir + os.sep): |
| 141 | output.append(line[len(test_temp_dir + os.sep) :].rstrip("\r\n")) |
| 142 | else: |
| 143 | # Normalize paths so that the output is the same on Windows and Linux/macOS. |
| 144 | # Yes, this is naive: replace all slashes preceding first colon, if any. |
| 145 | path, *rest = line.split(":", maxsplit=1) |
| 146 | if rest: |
| 147 | path = path.replace(os.sep, "/") |
| 148 | output.append(":".join([path, *rest]).rstrip("\r\n")) |
no test coverage detected
searching dependent graphs…