8000 gh-134235: Import Autocomplete for Builtin Modules (GH-134277) · faster-cpython/cpython@8421b03 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8421b03

Browse files
tommix626hyoung3
andauthored
pythongh-134235: Import Autocomplete for Builtin Modules (pythonGH-134277)
* added enhancement auto completing import with sys builtins --------- Co-authored-by: Hunter <hyoung3@gmail.com>
1 parent 4709417 commit 8421b03

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

Lib/_pyrepl/_module_completer.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ def find_modules(self, path: str, prefix: str) -> list[str]:
8181
def _find_modules(self, path: str, prefix: str) -> list[str]:
8282
if not path:
8383
# Top-level import (e.g. `import foo<tab>`` or `from foo<tab>`)`
84-
return [name for _, name, _ in self.global_cache
85-
if name.startswith(prefix)]
84+
builtin_modules = [name for name in sys.builtin_module_names if name.startswith(prefix)]
85+
third_party_modules = [name for _, name, _ in self.global_cache if name.startswith(prefix)]
86+
return sorted(builtin_modules + third_party_modules)
8687

8788
if path.startswith('.'):
8889
# Convert relative path to absolute path

Lib/test/test_pyrepl/test_pyrepl.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,26 @@ def test_import_completions(self):
959959
output = reader.readline()
960960
self.assertEqual(output, expected)
961961

962+
def test_builtin_completion_top_level(self):
963+
import importlib
964+
# Make iter_modules() search only the standard library.
965+
# This makes the test more reliable in case there are
966+
# other user packages/scripts on PYTHONPATH which can
967+
# intefere with the completions.
968+
lib_path = os.path.dirname(importlib.__path__[0])
969+
sys.path = [lib_path]
970+
971+
cases = (
972+
("import bui\t\n", "import builtins"),
973+
("from bui\t\n", "from builtins"),
974+
)
975+
for code, expected in cases:
976+
with self.subTest(code=code):
977+
events = code_to_events(code)
978+
reader = self.prepare_reader(events, namespace={})
979+
output = reader.readline()
980+
self.assertEqual(output, expected)
981+
962982
def test_relative_import_completions(self):
963983
cases = (
964984
("from .readl\t\n", "from .readline"),
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Updated tab completion on REPL to include builtin modules. Contributed by
2+
Tom Wang, Hunter Young

0 commit comments

Comments
 (0)
0