Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Lib/_pyrepl/_module_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@ def __init__(self, namespace: Mapping[str, Any] | None = None) -> None:
self._global_cache: list[pkgutil.ModuleInfo] = []
self._curr_sys_path: list[str] = sys.path[:]

def get_completions(self, line: str) -> list[str]:
"""Return the next possible import completions for 'line'."""
def get_completions(self, line: str) -> list[str] | None:
"""Return the next possible import completions for 'line'.

If 'line' is not an import statement, return None.
"""
result = ImportParser(line).parse()
if not result:
return []
return None
try:
return self.complete(*result)
except Exception:
Expand Down
4 changes: 2 additions & 2 deletions Lib/_pyrepl/readline.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def get_stem(self) -> str:
return "".join(b[p + 1 : self.pos])

def get_completions(self, stem: str) -> list[str]:
if module_completions := self.get_module_completions():
if (module_completions := self.get_module_completions()) is not None:
return module_completions
if len(stem) == 0 and self.more_lines is not None:
b = self.buffer
Expand Down Expand Up @@ -165,7 +165,7 @@ def get_completions(self, stem: str) -> list[str]:
result.sort()
return result

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

Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_pyrepl/test_pyrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,13 +944,16 @@ def test_import_completions(self):
("import importlib.resources.\t\ta\t\n", "import importlib.resources.abc"),
("import foo, impo\t\n", "import foo, importlib"),
("import foo as bar, impo\t\n", "import foo as bar, importlib"),
("import pri\t\n", "import pri"), # do not complete with "print("
("from impo\t\n", "from importlib"),
("from pri\t\n", "from pri"),
("from importlib.res\t\n", "from importlib.resources"),
("from importlib.\t\tres\t\n", "from importlib.resources"),
("from importlib.resources.ab\t\n", "from importlib.resources.abc"),
("from importlib import mac\t\n", "from importlib import machinery"),
("from importlib import res\t\n", "from importlib import resources"),
("from importlib.res\t import a\t\n", "from importlib.resources import abc"),
("from typing import Na\t\n", "from typing import Na"), # do not complete with "NameError("
)
for code, expected in cases:
with self.subTest(code=code):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Stop import names completion fallback on regular names completion in the :term:`REPL`.
Loading