Skip to content

Commit 7216f7b

Browse files
miss-islingtonLoïc Simon
andauthored
[3.14] gh-69605: Disable PyREPL module autocomplete fallback on regular completion (gh-134181) (gh-134680)
(cherry picked from commit 0e3bc96) Co-authored-by: Loïc Simon <[email protected]>
1 parent 1822f33 commit 7216f7b

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

Lib/_pyrepl/_module_completer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ def __init__(self, namespace: Mapping[str, Any] | None = None) -> None:
4242
self._global_cache: list[pkgutil.ModuleInfo] = []
4343
self._curr_sys_path: list[str] = sys.path[:]
4444

45-
def get_completions(self, line: str) -> list[str]:
45+
def get_completions(self, line: str) -> list[str] | None:
4646
"""Return the next possible import completions for 'line'."""
4747
result = ImportParser(line).parse()
4848
if not result:
49-
return []
49+
return None
5050
try:
5151
return self.complete(*result)
5252
except Exception:

Lib/_pyrepl/readline.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ def get_stem(self) -> str:
134134
return "".join(b[p + 1 : self.pos])
135135

136136
def get_completions(self, stem: str) -> list[str]:
137-
if module_completions := self.get_module_completions():
137+
module_completions = self.get_module_completions()
138+
if module_completions is not None:
138139
return module_completions
139140
if len(stem) == 0 and self.more_lines is not None:
140141
b = self.buffer
@@ -165,7 +166,7 @@ def get_completions(self, stem: str) -> list[str]:
165166
result.sort()
166167
return result
167168

168-
def get_module_completions(self) -> list[str]:
169+
def get_module_completions(self) -> list[str] | None:
169170
line = self.get_line()
170171
return self.config.module_completer.get_completions(line)
171172

Lib/test/test_pyrepl/test_pyrepl.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,14 @@ def test_func(self):
918918

919919
class TestPyReplModuleCompleter(TestCase):
920920
def setUp(self):
921+
import importlib
922+
# Make iter_modules() search only the standard library.
923+
# This makes the test more reliable in case there are
924+
# other user packages/scripts on PYTHONPATH which can
925+
# interfere with the completions.
926+
lib_path = os.path.dirname(importlib.__path__[0])
921927
self._saved_sys_path = sys.path
928+
sys.path = [lib_path]
922929

923930
def tearDown(self):
924931
sys.path = self._saved_sys_path
@@ -932,14 +939,6 @@ def prepare_reader(self, events, namespace):
932939
return reader
933940

934941
def test_import_completions(self):
935-
import importlib
936-
# Make iter_modules() search only the standard library.
937-
# This makes the test more reliable in case there are
938-
# other user packages/scripts on PYTHONPATH which can
939-
# intefere with the completions.
940-
lib_path = os.path.dirname(importlib.__path__[0])
941-
sys.path = [lib_path]
942-
943942
cases = (
944943
("import path\t\n", "import pathlib"),
945944
("import importlib.\t\tres\t\n", "import importlib.resources"),
@@ -1052,6 +1051,19 @@ def test_invalid_identifiers(self):
10521051
output = reader.readline()
10531052
self.assertEqual(output, expected)
10541053

1054+
def test_no_fallback_on_regular_completion(self):
1055+
cases = (
1056+
("import pri\t\n", "import pri"),
1057+
("from pri\t\n", "from pri"),
1058+
("from typing import Na\t\n", "from typing import Na"),
1059+
)
1060+
for code, expected in cases:
1061+
with self.subTest(code=code):
1062+
events = code_to_events(code)
1063+
reader = self.prepare_reader(events, namespace={})
1064+
output = reader.readline()
1065+
self.assertEqual(output, expected)
1066+
10551067
def test_get_path_and_prefix(self):
10561068
cases = (
10571069
('', ('', '')),
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
When auto-completing an import in the :term:`REPL`, finding no candidates
2+
now issues no suggestion, rather than suggestions from the current namespace.

0 commit comments

Comments
 (0)