Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
23 changes: 22 additions & 1 deletion Lib/_pyrepl/_module_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def get_completions(self, line: str) -> list[str] | None:
if not result:
return None
try:
return self.complete(*result)
names = self.complete(*result)
return [self._remove_separated_words(line, c) for c in names]
except Exception:
# Some unexpected error occurred, make it look like
# no completions are available
Expand Down Expand Up @@ -211,6 +212,26 @@ def global_cache(self) -> list[pkgutil.ModuleInfo]:
self._global_cache = list(pkgutil.iter_modules())
return self._global_cache

def _remove_separated_words(self, line: str, completion: str) -> str:
"""Remove completion parts imputed as separate words.
Needed because the completer insert any completion provided
by replacing the stem (eg. word) currently imputed, if any.
Examples:
- 'import foo.', 'foo.bar' -> 'foo.bar'
- 'import foo .', 'foo.bar' -> '.bar'
- 'from foo import ', 'bar' -> 'bar'
- 'import x.x .x.', 'x.x.x.x' -> '.x.x'
"""
last_word = line.split(" ")[-1]
if not last_word:
return completion
index = completion.rfind(last_word)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With apkg/foofoo.py, import apkg. foo<Tab> completes to foo. rfind() picks the second occurrence. Can we use the parser's replacement boundary here? This also affects main and 3.15.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@loic-simon This is a backport of your #148629, would you like to followup here? Thanks!

if index > 0:
return completion[index:]
return completion


class ImportParser:
"""
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_pyrepl/test_pyrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,12 @@ def test_import_completions(self):
("from importlib import res\t\n", "from importlib import resources"),
("from importlib.res\t import a\t\n", "from importlib.resources import abc"),
("from __phello__ import s\t\n", "from __phello__ import spam"), # frozen module
("import importlib .res\t\n", "import importlib .resources"),
("import importlib. res\t\n", "import importlib. resources"),
("import importlib . res\t\n", "import importlib . resources"),
("import importlib .metadata.\t\n", "import importlib .metadata.diagnose"),
("import importlib .metadata .\t\n", "import importlib .metadata .diagnose"),
("from importlib .resources .a\t\n", "from importlib .resources .abc"),
)
for code, expected in cases:
with self.subTest(code=code):
Expand Down Expand Up @@ -1313,8 +1319,13 @@ def test_parse(self):
('import a.b.c, foo.bar, ', (None, '')),
('from foo', ('foo', None)),
('from a.', ('a.', None)),
('from a .', ('a.', None)),
('from a .', ('a.', None)),
('from a .', ('a.', None)),
('from a.b', ('a.b', None)),
('from a.b.', ('a.b.', None)),
('from a. b.', ('a.b.', None)),
('from a . b .', ('a.b.', None)),
('from a.b.c', ('a.b.c', None)),
('from foo import ', ('foo', '')),
('from foo import a', ('foo', 'a')),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :term:`REPL` completions inserted incorrectly when there was spaces in
import statements.
Loading