Skip to content

Commit 1013912

Browse files
loic-simonpablogsal
authored andcommitted
Use always available modules in tests + invalidate caches
1 parent 3bb88d1 commit 1013912

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
@@ -1315,29 +1315,23 @@ def test_already_imported_module_without_origin_or_spec(self):
13151315
self.assertEqual(output, f"import {mod}.")
13161316
del sys.modules[mod]
13171317

1318-
def test_attribute_completion_module_already_imported(self):
1319-
cases = (
1320-
("from collections import def\t\n", "from collections import defaultdict"),
1321-
("from collections.abc import \tB\t\n", "from collections.abc import Buffer"),
1322-
)
1323-
for code, expected in cases:
1324-
with self.subTest(code=code):
1325-
events = code_to_events(code)
1326-
reader = self.prepare_reader(events, namespace={})
1327-
output = reader.readline()
1328-
self.assertEqual(output, expected)
1329-
1330-
def test_attribute_completion_module_on_demand(self):
1318+
@patch.dict(sys.modules)
1319+
def test_attribute_completion(self):
13311320
with tempfile.TemporaryDirectory() as _dir:
13321321
dir = pathlib.Path(_dir)
13331322
(dir / "foo.py").write_text("bar = 42")
1323+
(dir / "bar.py").write_text("baz = 42")
13341324
(dir / "pack").mkdir()
13351325
(dir / "pack" / "__init__.py").write_text("attr = 42")
13361326
(dir / "pack" / "foo.py").touch()
13371327
(dir / "pack" / "bar.py").touch()
13381328
(dir / "pack" / "baz.py").touch()
13391329
sys.modules.pop("graphlib", None) # test modules may have been imported by previous tests
1330+
sys.modules.pop("antigravity", None)
1331+
sys.modules.pop("unittest.__main__", None)
13401332
with patch.object(sys, "path", [_dir, *sys.path]):
1333+
pkgutil.get_importer(_dir).invalidate_caches()
1334+
importlib.import_module("bar")
13411335
cases = (
13421336
# needs 2 tabs to import (show prompt, then import)
13431337
("from foo import \t\n", "from foo import ", set()),
@@ -1357,6 +1351,8 @@ def test_attribute_completion_module_on_demand(self):
13571351
("from pack import b\t\n", "from pack import ba", set()),
13581352
("from pack import b\t\t\n", "from pack import ba", set()),
13591353
("from pack import b\t\t\t\n", "from pack import ba", {"pack"}),
1354+
# module already imported
1355+
("from bar import b\t\n", "from bar import baz", set()),
13601356
# stdlib modules are automatically imported
13611357
("from graphlib import T\t\n", "from graphlib import TopologicalSorter", {"graphlib"}),
13621358
# except those with known side-effects
@@ -1573,8 +1569,9 @@ def test_suggestions_and_messages(self) -> None:
15731569
(dir / "pack" / "__init__.py").write_text("foo = 1; bar = 2;")
15741570
(dir / "pack" / "bar.py").touch()
15751571
sys.modules.pop("graphlib", None) # test modules may have been imported by previous tests
1576-
sys.modules.pop("compression.zstd", None)
1572+
sys.modules.pop("html.entities", None)
15771573
with patch.object(sys, "path", [_dir, *sys.path]):
1574+
pkgutil.get_importer(_dir).invalidate_caches()
15781575
cases = (
15791576
# no match != not an import
15801577
("import nope", ([], None), set()),
@@ -1597,7 +1594,7 @@ def test_suggestions_and_messages(self) -> None:
15971594
("from pack.bar import ", ([], None), set()),
15981595
# stdlib = auto-imported
15991596
("from graphlib import T", (["TopologicalSorter"], None), {"graphlib"}),
1600-
("from compression.zstd import c", (["compress"], None), {"compression.zstd"}),
1597+
("from html.entities import h", (["html5"], None), {"html", "html.entities"}),
16011598
)
16021599
completer = ModuleCompleter()
16031600
for i, (code, expected, expected_imports) in enumerate(cases):

0 commit comments

Comments
 (0)