Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8421b03

Browse files
tommix626hyoung3
andauthored
gh-134235: Import Autocomplete for Builtin Modules (GH-134277)
* added enhancement auto completing import with sys builtins---------Co-authored-by: Hunter <hyoung3@gmail.com>
1 parent4709417 commit8421b03

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
ifnotpath:
8383
# Top-level import (e.g. `import foo<tab>`` or `from foo<tab>`)`
84-
return [namefor_,name,_inself.global_cache
85-
ifname.startswith(prefix)]
84+
builtin_modules= [namefornameinsys.builtin_module_namesifname.startswith(prefix)]
85+
third_party_modules= [namefor_,name,_inself.global_cacheifname.startswith(prefix)]
86+
returnsorted(builtin_modules+third_party_modules)
8687

8788
ifpath.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+
deftest_builtin_completion_top_level(self):
963+
importimportlib
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+
forcode,expectedincases:
976+
withself.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
deftest_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)

[8]ページ先頭

©2009-2025 Movatter.jp