Skip to content

Commit 97239bc

Browse files
committed
gh-69605: fix PyREPL completions inserted for import statements with spaces edge cases
1 parent 31ed3ef commit 97239bc

3 files changed

Lines changed: 41 additions & 1 deletion

File tree

‎Lib/_pyrepl/_module_completer.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,14 @@ def _remove_separated_words(self, line: str, completion: str) -> str:
362362
last_word = line.split(" ")[-1]
363363
if not last_word:
364364
return completion
365+
if last_word.startswith("."):
366+
offset = 0
367+
else: # Only match the start of module parts
368+
last_word = f".{last_word}"
369+
offset = 1
365370
index = completion.rfind(last_word)
366371
if index > 0:
367-
return completion[index:]
372+
return completion[index + offset:]
368373
return completion
369374

370375

‎Lib/test/test_pyrepl/test_pyrepl.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,39 @@ def test_completions(self):
12701270
output = reader.readline()
12711271
self.assertEqual(output, expected)
12721272

1273+
@patch("pkgutil.iter_modules", lambda: [ModuleInfo(None, "apkg", True)])
1274+
@patch(
1275+
"_pyrepl._module_completer.ModuleCompleter.iter_submodules",
1276+
lambda *_: [
1277+
ModuleInfo(None, "foofoo", False),
1278+
ModuleInfo(None, "bar", False),
1279+
ModuleInfo(None, "barbar", False),
1280+
],
1281+
)
1282+
def test_import_completions_with_spaces(self):
1283+
cases = (
1284+
("import ap\t\n", "import apkg"),
1285+
("import apkg .f\t\n", "import apkg .foofoo"),
1286+
("import apkg. f\t\n", "import apkg. foofoo"),
1287+
("import apkg . f\t\n", "import apkg . foofoo"),
1288+
("import apkg .b\t\n", "import apkg .bar"),
1289+
("import apkg. b\t\n", "import apkg. bar"),
1290+
("import apkg . b\t\n", "import apkg . bar"),
1291+
("import apkg .barb\t\n", "import apkg .barbar"),
1292+
("import apkg. barb\t\n", "import apkg. barbar"),
1293+
("import apkg .bar .f\t\n", "import apkg .bar .foofoo"),
1294+
("import apkg .bar. f\t\n", "import apkg .bar. foofoo"),
1295+
("import apkg .bar .b\t\n", "import apkg .bar .bar"),
1296+
("import apkg .bar. b\t\n", "import apkg .bar. bar"),
1297+
("import apkg . bar . barb\t\n", "import apkg . bar . barbar"),
1298+
)
1299+
for code, expected in cases:
1300+
with self.subTest(code=code):
1301+
events = code_to_events(code)
1302+
reader = self.prepare_reader(events, namespace={})
1303+
output = reader.readline()
1304+
self.assertEqual(output, expected)
1305+
12731306
@patch("pkgutil.iter_modules", lambda: [ModuleInfo(None, "public", True),
12741307
ModuleInfo(None, "_private", True)])
12751308
@patch("sys.builtin_module_names", ())
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :term:`REPL` completions inserted incorrectly in some edge cases
2+
when there was spaces in import statements.

0 commit comments

Comments
 (0)