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

gh-134215: PyREPL: Do not show underscored modules by default during autocompletion#134267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ambv merged 26 commits intopython:mainfromkevteg:pyrepl_underscored_fix
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
26 commits
Select commitHold shift + click to select a range
fd0212d
gh-134215: Improve ModuleCompleter module
kevtegMay 19, 2025
3e314e8
Merge branch 'main' into pyrepl_underscored_fix
kevtegMay 19, 2025
7c4f4c8
Remove unused import
kevtegMay 19, 2025
ff6b6a3
Merge branch 'pyrepl_underscored_fix' of github.com:kevteg/cpython in…
kevtegMay 19, 2025
f7a2db9
Add news
kevtegMay 19, 2025
2c5f81e
Rename method
kevtegMay 19, 2025
1472148
Update method name
kevtegMay 19, 2025
5f97959
Feedback
kevtegMay 19, 2025
a04ecdf
Feedback
kevtegMay 19, 2025
ca89754
Update docstring
kevtegMay 19, 2025
5636adb
Merge branch 'main' into pyrepl_underscored_fix
kevtegMay 19, 2025
dc1a403
Merge branch 'pyrepl_underscored_fix' of github.com:kevteg/cpython in…
kevtegMay 19, 2025
4a2872a
Update tests
kevtegMay 19, 2025
e854d10
Update Lib/test/test_pyrepl/test_pyrepl.py
kevtegMay 19, 2025
e3fc8e0
Update Misc/NEWS.d/next/Tools-Demos/2025-05-19-14-57-46.gh-issue-1342…
kevtegMay 19, 2025
4918343
Fix
kevtegMay 19, 2025
04f78cf
Merge branch 'pyrepl_underscored_fix' of github.com:kevteg/cpython in…
kevtegMay 19, 2025
ff02e15
Merge branch 'main' into pyrepl_underscored_fix
kevtegMay 20, 2025
262b4e1
Merge branch 'main' into pyrepl_underscored_fix
kevtegMay 20, 2025
0323ec3
Small refactor
kevtegMay 20, 2025
038fc4e
Merge branch 'pyrepl_underscored_fix' of github.com:kevteg/cpython in…
kevtegMay 20, 2025
76435a0
Merge branch 'main' into pyrepl_underscored_fix
kevtegMay 20, 2025
14cac01
Remove docstring
kevtegMay 20, 2025
e1ac435
Merge branch 'pyrepl_underscored_fix' of github.com:kevteg/cpython in…
kevtegMay 20, 2025
1e88864
Update Lib/_pyrepl/_module_completer.py
ambvMay 20, 2025
99a3d6b
Merge branch 'main' into pyrepl_underscored_fix
ambvMay 20, 2025
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
NextNext commit
gh-134215: Improve ModuleCompleter module
* Add _should_add_module_name method to the ModuleCompleter module* Add new tests to check autocomplete behaviour on private method
  • Loading branch information
@kevteg
kevteg committedMay 19, 2025
commitfd0212d039571aa72747dd8b473226427a8c321f
12 changes: 10 additions & 2 deletionsLib/_pyrepl/_module_completer.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,6 +9,7 @@
from dataclasses import dataclass
from itertools import chain
from tokenize import TokenInfo
from .trace import trace

TYPE_CHECKING = False

Expand DownExpand Up@@ -74,6 +75,7 @@ def complete(self, from_name: str | None, name: str | None) -> list[str]:
def find_modules(self, path: str, prefix: str) -> list[str]:
"""Find all modules under 'path' that start with 'prefix'."""
modules = self._find_modules(path, prefix)
trace(">> path {path} prefix {prefix}", path=path, prefix=prefix)
# Filter out invalid module names
# (for example those containing dashes that cannot be imported with 'import')
return [mod for mod in modules if mod.isidentifier()]
Expand All@@ -82,7 +84,7 @@ def _find_modules(self, path: str, prefix: str) -> list[str]:
if not path:
# Top-level import (e.g. `import foo<tab>`` or `from foo<tab>`)`
return [name for _, name, _ in self.global_cache
ifname.startswith(prefix)]
ifself._should_add_module_name(name,prefix)]

if path.startswith('.'):
# Convert relative path to absolute path
Expand All@@ -96,8 +98,14 @@ def _find_modules(self, path: str, prefix: str) -> list[str]:
modules = [mod_info for mod_info in modules
if mod_info.ispkg and mod_info.name == segment]
modules = self.iter_submodules(modules)

return [module.name for module in modules
if module.name.startswith(prefix)]
if self._should_add_module_name(module.name, prefix)]

def _should_add_module_name(self, module_name: str, prefix: str) -> bool:
prefix_modules = prefix and module_name.startswith(prefix)
public_modules = not prefix and not module_name.startswith("_")
return prefix_modules or public_modules

def iter_submodules(self, parent_modules: list[pkgutil.ModuleInfo]) -> Iterator[pkgutil.ModuleInfo]:
"""Iterate over all submodules of the given parent modules."""
Expand Down
40 changes: 40 additions & 0 deletionsLib/test/test_pyrepl/test_pyrepl.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,6 +8,7 @@
import subprocess
import sys
import tempfile
from pkgutil import ModuleInfo
from unittest import TestCase, skipUnless, skipIf
from unittest.mock import patch
from test.support import force_not_colorized, make_clean_env
Expand DownExpand Up@@ -959,6 +960,45 @@ def test_import_completions(self):
output = reader.readline()
self.assertEqual(output, expected)

@patch("pkgutil.iter_modules", lambda: [(None, 'public', None),
(None, '_private', None)])
def test_private_completions(self):
cases = (
# Return public methods by default
("import \t\n", "import public"),
("from \t\n", "from public"),
# Return private methods if explicitly specified
("import _\t\n", "import _private"),
("from _\t\n", "from _private"),
)
for code, expected in cases:
with self.subTest(code=code):
events = code_to_events(code)
reader = self.prepare_reader(events, namespace={})
output = reader.readline()
self.assertEqual(output, expected)

@patch(
"_pyrepl._module_completer.ModuleCompleter.iter_submodules",
lambda *_: [
ModuleInfo(None, "public", True),
ModuleInfo(None, "_private", True),
],
)
def test_sub_module_private_completions(self):
cases = (
# Return public methods by default
("from foo import \t\n", "from foo import public"),
# Return private methods if explicitly specified
("from foo import _\t\n", "from foo import _private"),
)
for code, expected in cases:
with self.subTest(code=code):
events = code_to_events(code)
reader = self.prepare_reader(events, namespace={})
output = reader.readline()
self.assertEqual(output, expected)

def test_relative_import_completions(self):
cases = (
("from .readl\t\n", "from .readline"),
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp