|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import os |
| 6 | +import shutil |
5 | 7 | import sys |
| 8 | +import tempfile |
6 | 9 |
|
| 10 | +import pytest |
7 | 11 | from pytest import skip |
8 | 12 |
|
9 | | -from mypy import defaults |
| 13 | +from mypy import build, defaults |
10 | 14 | from mypy.config_parser import parse_mypy_comments |
11 | 15 | from mypy.errors import CompileError, Errors |
| 16 | +from mypy.modulefinder import BuildSource |
12 | 17 | from mypy.options import Options |
13 | 18 | from mypy.parse import parse |
14 | 19 | from mypy.test.data import DataDrivenTestCase, DataSuite |
15 | | -from mypy.test.helpers import assert_string_arrays_equal, find_test_files, parse_options |
| 20 | +from mypy.test.helpers import Suite, assert_string_arrays_equal, find_test_files, parse_options |
16 | 21 | from mypy.test.update_data import update_testcase_output |
17 | 22 | from mypy.util import get_mypy_comments |
18 | 23 |
|
@@ -129,3 +134,66 @@ def test_parse_error(testcase: DataDrivenTestCase) -> None: |
129 | 134 | e.messages, |
130 | 135 | f"Invalid compiler output ({testcase.file}, line {testcase.line})", |
131 | 136 | ) |
| 137 | + |
| 138 | + |
| 139 | +class TransformSourceSuite(Suite): |
| 140 | + """Tests for options.transform_source (see #21222). |
| 141 | +
|
| 142 | + transform_source is a Python callable, so it cannot be exercised via the |
| 143 | + data-driven test cases; these are plain unit tests instead. |
| 144 | + """ |
| 145 | + |
| 146 | + def parse_with_transform(self, native_parser: bool) -> int: |
| 147 | + """Parse a trivial module with transform_source set. |
| 148 | +
|
| 149 | + Return the number of top-level definitions in the resulting AST. |
| 150 | + """ |
| 151 | + options = Options() |
| 152 | + options.native_parser = native_parser |
| 153 | + options.transform_source = lambda source: source + "reveal_type(1)\n" |
| 154 | + errors = Errors(options) |
| 155 | + tree = parse( |
| 156 | + "x = 1\n", fnam="main", module="__main__", errors=errors, options=options, eager=True |
| 157 | + ) |
| 158 | + assert not errors.is_errors() |
| 159 | + return len(tree.defs) |
| 160 | + |
| 161 | + def test_transform_source_native_parser(self) -> None: |
| 162 | + pytest.importorskip("ast_serialize") |
| 163 | + assert Options().native_parser |
| 164 | + # The transform appends a statement, so we expect two top-level definitions. |
| 165 | + assert self.parse_with_transform(native_parser=True) == 2 |
| 166 | + |
| 167 | + def test_transform_source_old_parser(self) -> None: |
| 168 | + # The transform appends a statement, so we expect two top-level definitions. |
| 169 | + assert self.parse_with_transform(native_parser=False) == 2 |
| 170 | + |
| 171 | + def test_transform_source_parallel_build(self) -> None: |
| 172 | + """transform_source must be honored with parallel (native parser) checking. |
| 173 | +
|
| 174 | + With more than one file the native parser parses in parallel threads, and |
| 175 | + the source must be read (and transformed) instead of letting the parser |
| 176 | + use the file on disk directly. |
| 177 | + """ |
| 178 | + pytest.importorskip("ast_serialize") |
| 179 | + assert Options().native_parser |
| 180 | + tmpdir = tempfile.mkdtemp(prefix="mypy-test-transform-") |
| 181 | + self.addCleanup(shutil.rmtree, tmpdir, ignore_errors=True) |
| 182 | + for name in ("a.py", "b.py"): |
| 183 | + with open(os.path.join(tmpdir, name), "w") as f: |
| 184 | + f.write("x: int = 1\n") |
| 185 | + options = Options() |
| 186 | + # The transform introduces a type error on line 3 of each file. |
| 187 | + options.transform_source = lambda source: source + "\nbad: str = 1\n" |
| 188 | + options.incremental = False |
| 189 | + options.cache_dir = os.devnull |
| 190 | + options.show_traceback = True |
| 191 | + sources = [ |
| 192 | + BuildSource(os.path.join(tmpdir, name), name[:-3], None) for name in ("a.py", "b.py") |
| 193 | + ] |
| 194 | + try: |
| 195 | + result = build.build(sources=sources, options=options) |
| 196 | + messages = result.errors |
| 197 | + except CompileError as e: |
| 198 | + messages = e.messages |
| 199 | + assert len([m for m in messages if ":3: error" in m]) == 2 |
0 commit comments