From 38baa51af25e0af836b25523139e0b0e437484e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Simon?= Date: Wed, 21 Oct 2026 01:23:13 +0200 Subject: [PATCH] [3.14] gh-69605: fix PyREPL completions inserted for import statements with spaces (GH-148629) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix `from math .` inserting `from math .ath.integer` instead of `from math.integer` (cherry picked from commit 488ce0f050d798ff0553ac6b52e7bccb73b60017) Co-authored-by: Loïc Simon --- Lib/_pyrepl/_module_completer.py | 23 ++++++++++++++++++- Lib/test/test_pyrepl/test_pyrepl.py | 11 +++++++++ ...6-04-15-21-55-45.gh-issue-69605.NwoP6l.rst | 2 ++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-04-15-21-55-45.gh-issue-69605.NwoP6l.rst diff --git a/Lib/_pyrepl/_module_completer.py b/Lib/_pyrepl/_module_completer.py index 2098d0a54aba316..5e71bf482cbc524 100644 --- a/Lib/_pyrepl/_module_completer.py +++ b/Lib/_pyrepl/_module_completer.py @@ -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 @@ -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) + if index > 0: + return completion[index:] + return completion + class ImportParser: """ diff --git a/Lib/test/test_pyrepl/test_pyrepl.py b/Lib/test/test_pyrepl/test_pyrepl.py index df2e0f4af1ac769..412baea6242c249 100644 --- a/Lib/test/test_pyrepl/test_pyrepl.py +++ b/Lib/test/test_pyrepl/test_pyrepl.py @@ -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): @@ -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')), diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-04-15-21-55-45.gh-issue-69605.NwoP6l.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-04-15-21-55-45.gh-issue-69605.NwoP6l.rst new file mode 100644 index 000000000000000..77491c6e5f62070 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-04-15-21-55-45.gh-issue-69605.NwoP6l.rst @@ -0,0 +1,2 @@ +Fix :term:`REPL` completions inserted incorrectly when there was spaces in +import statements.