Skip to content

Commit 935e3fd

Browse files
committed
Use always available modules in tests + invalidate caches
1 parent 9c80367 commit 935e3fd

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

Lib/test/test_pyrepl/test_pyrepl.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,29 +1223,23 @@ def test_already_imported_module_without_origin_or_spec(self):
12231223
self.assertEqual(output, f"import {mod}.")
12241224
del sys.modules[mod]
12251225

1226-
def test_attribute_completion_module_already_imported(self):
1227-
cases = (
1228-
("from collections import def\t\n", "from collections import defaultdict"),
1229-
("from collections.abc import \tB\t\n", "from collections.abc import Buffer"),
1230-
)
1231-
for code, expected in cases:
1232-
with self.subTest(code=code):
1233-
events = code_to_events(code)
1234-
reader = self.prepare_reader(events, namespace={})
1235-
output = reader.readline()
1236-
self.assertEqual(output, expected)
1237-
1238-
def test_attribute_completion_module_on_demand(self):
1226+
@patch.dict(sys.modules)
1227+
def test_attribute_completion(self):
12391228
with tempfile.TemporaryDirectory() as _dir:
12401229
dir = pathlib.Path(_dir)
12411230
(dir / "foo.py").write_text("bar = 42")
1231+
(dir / "bar.py").write_text("baz = 42")
12421232
(dir / "pack").mkdir()
12431233
(dir / "pack" / "__init__.py").write_text("attr = 42")
12441234
(dir / "pack" / "foo.py").touch()
12451235
(dir / "pack" / "bar.py").touch()
12461236
(dir / "pack" / "baz.py").touch()
12471237
sys.modules.pop("graphlib", None) # test modules may have been imported by previous tests
1238+
sys.modules.pop("antigravity", None)
1239+
sys.modules.pop("unittest.__main__", None)
12481240
with patch.object(sys, "path", [_dir, *sys.path]):
1241+
pkgutil.get_importer(_dir).invalidate_caches()
1242+
importlib.import_module("bar")
12491243
cases = (
12501244
# needs 2 tabs to import (show prompt, then import)
12511245
("from foo import \t\n", "from foo import ", set()),
@@ -1265,6 +1259,8 @@ def test_attribute_completion_module_on_demand(self):
12651259
("from pack import b\t\n", "from pack import ba", set()),
12661260
("from pack import b\t\t\n", "from pack import ba", set()),
12671261
("from pack import b\t\t\t\n", "from pack import ba", {"pack"}),
1262+
# module already imported
1263+
("from bar import b\t\n", "from bar import baz", set()),
12681264
# stdlib modules are automatically imported
12691265
("from graphlib import T\t\n", "from graphlib import TopologicalSorter", {"graphlib"}),
12701266
# except those with known side-effects
@@ -1480,8 +1476,9 @@ def test_suggestions_and_messages(self) -> None:
14801476
(dir / "pack" / "__init__.py").write_text("foo = 1; bar = 2;")
14811477
(dir / "pack" / "bar.py").touch()
14821478
sys.modules.pop("graphlib", None) # test modules may have been imported by previous tests
1483-
sys.modules.pop("compression.zstd", None)
1479+
sys.modules.pop("html.entities", None)
14841480
with patch.object(sys, "path", [_dir, *sys.path]):
1481+
pkgutil.get_importer(_dir).invalidate_caches()
14851482
cases = (
14861483
# no match != not an import
14871484
("import nope", ([], None), set()),
@@ -1504,7 +1501,7 @@ def test_suggestions_and_messages(self) -> None:
15041501
("from pack.bar import ", ([], None), set()),
15051502
# stdlib = auto-imported
15061503
("from graphlib import T", (["TopologicalSorter"], None), {"graphlib"}),
1507-
("from compression.zstd import c", (["compress"], None), {"compression.zstd"}),
1504+
("from html.entities import h", (["html5"], None), {"html", "html.entities"}),
15081505
)
15091506
completer = ModuleCompleter()
15101507
for i, (code, expected, expected_imports) in enumerate(cases):

0 commit comments

Comments
 (0)