Skip to content

Commit 8978e95

Browse files
committed
PyREPL module autocomplete: do not fallback
When no module completions are available, do not fallback to completions from current namespace
1 parent 84914ad commit 8978e95

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

Lib/_pyrepl/_module_completer.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,14 @@ 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]:
46-
"""Return the next possible import completions for 'line'."""
45+
def get_completions(self, line: str) -> list[str] | None:
46+
"""Return the next possible import completions for 'line'.
47+
48+
If 'line' is not an import statement, return None.
49+
"""
4750
result = ImportParser(line).parse()
4851
if not result:
49-
return []
52+
return None
5053
try:
5154
return self.complete(*result)
5255
except Exception:

Lib/_pyrepl/readline.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ 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+
if (module_completions := self.get_module_completions()) is not None:
138138
return module_completions
139139
if len(stem) == 0 and self.more_lines is not None:
140140
b = self.buffer
@@ -165,7 +165,7 @@ def get_completions(self, stem: str) -> list[str]:
165165
result.sort()
166166
return result
167167

168-
def get_module_completions(self) -> list[str]:
168+
def get_module_completions(self) -> list[str] | None:
169169
line = self.get_line()
170170
return self.config.module_completer.get_completions(line)
171171

Lib/test/test_pyrepl/test_pyrepl.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,13 +944,16 @@ def test_import_completions(self):
944944
("import importlib.resources.\t\ta\t\n", "import importlib.resources.abc"),
945945
("import foo, impo\t\n", "import foo, importlib"),
946946
("import foo as bar, impo\t\n", "import foo as bar, importlib"),
947+
("import pri\t\n", "import pri"), # do not complete with "print("
947948
("from impo\t\n", "from importlib"),
949+
("from pri\t\n", "from pri"),
948950
("from importlib.res\t\n", "from importlib.resources"),
949951
("from importlib.\t\tres\t\n", "from importlib.resources"),
950952
("from importlib.resources.ab\t\n", "from importlib.resources.abc"),
951953
("from importlib import mac\t\n", "from importlib import machinery"),
952954
("from importlib import res\t\n", "from importlib import resources"),
953955
("from importlib.res\t import a\t\n", "from importlib.resources import abc"),
956+
("from typing import Na\t\n", "from typing import Na"), # do not complete with "NameError("
954957
)
955958
for code, expected in cases:
956959
with self.subTest(code=code):

0 commit comments

Comments
 (0)