Skip to content

Commit 0a13de9

Browse files
committed
Simplify mypyc test
1 parent 1339671 commit 0a13de9

3 files changed

Lines changed: 37 additions & 64 deletions

File tree

mypyc/test-data/run-multimodule.test

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,39 @@ import native
12461246

12471247
[rechecked other_a]
12481248

1249+
-- Test that importing a skipped module does not force rechecks.
1250+
[case testIncrementalCompilationFollowImportsSkip]
1251+
from other_dep import f
1252+
assert f() == 1
1253+
1254+
[file other_dep.py]
1255+
import skipped
1256+
1257+
def f() -> int:
1258+
return 1
1259+
1260+
def g() -> int:
1261+
return 2
1262+
1263+
# Files under "skipped" are not typechecked because of the "--follow_imports = skip" option.
1264+
[file skipped/__init__.py]
1265+
x = 1
1266+
1267+
[file skipped/other_child.py]
1268+
import skipped
1269+
1270+
def child() -> int:
1271+
return 1
1272+
1273+
[file native.py.2]
1274+
from other_dep import g
1275+
assert g() == 2
1276+
1277+
[file driver.py]
1278+
import native
1279+
1280+
[rechecked native]
1281+
12491282
[case testSeparateCompilationWithUndefinedAttribute]
12501283
from other_a import A
12511284

mypyc/test/test_misc.py

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import os
44
import tempfile
55
import unittest
6-
from unittest.mock import patch
76

8-
import mypyc.build as mypyc_build_module
97
from mypyc.build import get_header_deps, resolve_cfile_deps
108
from mypyc.ir.ops import BasicBlock
119
from mypyc.ir.pprint import format_blocks, generate_names_for_ir
@@ -27,68 +25,6 @@ def test_debug_op(self) -> None:
2725
assert code[:-1] == ["L0:", " r0 = 'foo'", " CPyDebug_PrintObject(r0)"]
2826

2927

30-
class TestIncrementalOutputFiles(unittest.TestCase):
31-
def test_cached_dependency_output_file_not_overwritten_for_preserved_suppression(self) -> None:
32-
with tempfile.TemporaryDirectory() as tmp:
33-
build_dir = os.path.join(tmp, "build")
34-
with open(os.path.join(tmp, "mypy.ini"), "w", encoding="utf-8") as f:
35-
f.write("[mypy]\nfollow_imports = skip\n")
36-
os.mkdir(os.path.join(tmp, "skipped"))
37-
with open(os.path.join(tmp, "skipped", "__init__.py"), "w", encoding="utf-8") as f:
38-
f.write("x = 1\n")
39-
with open(os.path.join(tmp, "skipped", "child.py"), "w", encoding="utf-8") as f:
40-
f.write("import skipped\n\ndef child() -> int:\n return 1\n")
41-
with open(os.path.join(tmp, "dep.py"), "w", encoding="utf-8") as f:
42-
f.write("import skipped\n\ndef value() -> int:\n return 1\n")
43-
with open(os.path.join(tmp, "main.py"), "w", encoding="utf-8") as f:
44-
f.write("import dep\n\ndef value() -> int:\n return dep.value()\n")
45-
46-
compiler_options = CompilerOptions(separate=True, target_dir=build_dir)
47-
48-
old_cwd = os.getcwd()
49-
os.chdir(tmp)
50-
try:
51-
files = ["dep.py", "skipped/child.py", "main.py"]
52-
53-
groups, group_cfilenames, _ = mypyc_build_module.mypyc_build(
54-
files, compiler_options, separate=True
55-
)
56-
57-
dep_group = next(
58-
i
59-
for i, (group_sources, _) in enumerate(groups)
60-
if [source.module for source in group_sources] == ["dep"]
61-
)
62-
dep_output_files = {
63-
os.path.abspath(path)
64-
for paths in group_cfilenames[dep_group]
65-
for path in paths
66-
}
67-
68-
# Only main changed; dep should be loaded from the mypyc IR cache and reuse
69-
# its existing C outputs.
70-
with open("main.py", "w", encoding="utf-8") as f:
71-
f.write("import dep\n\ndef value() -> int:\n return dep.value() + 1\n")
72-
73-
written_paths: set[str] = set()
74-
original_write_file = mypyc_build_module.write_file
75-
76-
def recording_write_file(path: str, contents: str) -> None:
77-
written_paths.add(os.path.abspath(path))
78-
original_write_file(path, contents)
79-
80-
with patch.object(
81-
mypyc_build_module, "write_file", side_effect=recording_write_file
82-
):
83-
# skipped.child imports its skipped package ancestor; loading that cached
84-
# state should not make cached dep stale or rewrite dep's generated outputs.
85-
mypyc_build_module.mypyc_build(files, compiler_options, separate=True)
86-
finally:
87-
os.chdir(old_cwd)
88-
89-
assert written_paths.isdisjoint(dep_output_files)
90-
91-
9228
class TestHeaderDeps(unittest.TestCase):
9329
"""
9430
Tests for the header-dependency tracking used to build `Extension.depends`, which drives

mypyc/test/test_run.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ def run_case_step(self, testcase: DataDrivenTestCase, incremental_step: int) ->
234234
# Avoid checking modules/packages named 'unchecked', to provide a way
235235
# to test interacting with code we don't have types for.
236236
options.per_module_options["unchecked.*"] = {"follow_imports": "error"}
237+
# Avoid checking modules/packages named 'skipped', to provide a way
238+
# to test interacting with code ignored by follow_imports=skip.
239+
options.per_module_options["skipped"] = {"follow_imports": "skip"}
240+
options.per_module_options["skipped.*"] = {"follow_imports": "skip"}
237241

238242
source = build.BuildSource("native.py", "native", None)
239243
sources = [source]

0 commit comments

Comments
 (0)