mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
[3.14] gh-134215: PyREPL: Do not show underscored modules by default during autocompletion (gh-134267) (gh-134388)
(cherry picked from commit a3a3cf6d15
)
Co-authored-by: Kevin Hernández <kevin.hernandez@unet.edu.ve>
Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Co-authored-by: Tomas R. <tomas.roun8@gmail.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
This commit is contained in:
parent
04829d4d87
commit
69710b7087
4 changed files with 57 additions and 5 deletions
|
@ -8,6 +8,7 @@ import select
|
|||
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, Py_DEBUG
|
||||
|
@ -959,6 +960,46 @@ class TestPyReplModuleCompleter(TestCase):
|
|||
output = reader.readline()
|
||||
self.assertEqual(output, expected)
|
||||
|
||||
@patch("pkgutil.iter_modules", lambda: [ModuleInfo(None, "public", True),
|
||||
ModuleInfo(None, "_private", True)])
|
||||
@patch("sys.builtin_module_names", ())
|
||||
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_builtin_completion_top_level(self):
|
||||
import importlib
|
||||
# Make iter_modules() search only the standard library.
|
||||
|
@ -991,8 +1032,8 @@ class TestPyReplModuleCompleter(TestCase):
|
|||
output = reader.readline()
|
||||
self.assertEqual(output, expected)
|
||||
|
||||
@patch("pkgutil.iter_modules", lambda: [(None, 'valid_name', None),
|
||||
(None, 'invalid-name', None)])
|
||||
@patch("pkgutil.iter_modules", lambda: [ModuleInfo(None, "valid_name", True),
|
||||
ModuleInfo(None, "invalid-name", True)])
|
||||
def test_invalid_identifiers(self):
|
||||
# Make sure modules which are not valid identifiers
|
||||
# are not suggested as those cannot be imported via 'import'.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue