Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-69605: Hardcode some stdlib submodules in PyREPL module completion (os.path, collections.abc...)#138268
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ambv
merged 6 commits into
python:main
from
loic-simon:pyrepl-module-completion-hardcode-special-stdlib-submodulesSep 15, 2025
Uh oh!
There was an error while loading. Please reload this page.
Merged
gh-69605: Hardcode some stdlib submodules in PyREPL module completion (os.path, collections.abc...) #138268
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dc13676
PyREPL module completion: hardcode special stdlib submodules
loic-simon 6f38649
📜🤖 Added by blurb_it.
blurb-it[bot] 362e821
Review feedback
loic-simon a952c2a
Use .extend instead of +
loic-simon fbfe884
Do not propose hardcoded submodules if local import
loic-simon 985bb20
Nit nittety nit
ambv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import importlib | ||
| import io | ||
| import itertools | ||
| import os | ||
| @@ -26,9 +27,16 @@ | ||
| code_to_events, | ||
| ) | ||
| from _pyrepl.console import Event | ||
| from _pyrepl._module_completer import ImportParser, ModuleCompleter | ||
| from _pyrepl.readline import (ReadlineAlikeReader, ReadlineConfig, | ||
| _ReadlineWrapper) | ||
| from _pyrepl._module_completer import ( | ||
| ImportParser, | ||
| ModuleCompleter, | ||
| HARDCODED_SUBMODULES, | ||
| ) | ||
| from _pyrepl.readline import ( | ||
| ReadlineAlikeReader, | ||
| ReadlineConfig, | ||
| _ReadlineWrapper, | ||
| ) | ||
| from _pyrepl.readline import multiline_input as readline_multiline_input | ||
| try: | ||
| @@ -930,7 +938,6 @@ def test_func(self): | ||
| class TestPyReplModuleCompleter(TestCase): | ||
| def setUp(self): | ||
| import importlib | ||
| # Make iter_modules() search only the standard library. | ||
| # This makes the test more reliable in case there are | ||
| # other user packages/scripts on PYTHONPATH which can | ||
| @@ -1013,14 +1020,6 @@ def test_sub_module_private_completions(self): | ||
| self.assertEqual(output, expected) | ||
| def test_builtin_completion_top_level(self): | ||
| import importlib | ||
| # Make iter_modules() search only the standard library. | ||
| # This makes the test more reliable in case there are | ||
| # other user packages/scripts on PYTHONPATH which can | ||
| # intefere with the completions. | ||
| lib_path = os.path.dirname(importlib.__path__[0]) | ||
| sys.path = [lib_path] | ||
ambv marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| cases = ( | ||
| ("import bui\t\n", "import builtins"), | ||
| ("from bui\t\n", "from builtins"), | ||
| @@ -1076,6 +1075,32 @@ def test_no_fallback_on_regular_completion(self): | ||
| output = reader.readline() | ||
| self.assertEqual(output, expected) | ||
| def test_hardcoded_stdlib_submodules(self): | ||
| cases = ( | ||
| ("import collections.\t\n", "import collections.abc"), | ||
| ("from os import \t\n", "from os import path"), | ||
| ("import xml.parsers.expat.\t\te\t\n\n", "import xml.parsers.expat.errors"), | ||
| ("from xml.parsers.expat import \t\tm\t\n\n", "from xml.parsers.expat import model"), | ||
| ) | ||
| 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_hardcoded_stdlib_submodules_not_proposed_if_local_import(self): | ||
| with tempfile.TemporaryDirectory() as _dir: | ||
| dir = pathlib.Path(_dir) | ||
| (dir / "collections").mkdir() | ||
| (dir / "collections" / "__init__.py").touch() | ||
| (dir / "collections" / "foo.py").touch() | ||
| with patch.object(sys, "path", [dir, *sys.path]): | ||
| events = code_to_events("import collections.\t\n") | ||
| reader = self.prepare_reader(events, namespace={}) | ||
| output = reader.readline() | ||
| self.assertEqual(output, "import collections.foo") | ||
| def test_get_path_and_prefix(self): | ||
| cases = ( | ||
| ('', ('', '')), | ||
| @@ -1204,6 +1229,19 @@ def test_parse_error(self): | ||
| with self.subTest(code=code): | ||
| self.assertEqual(actual, None) | ||
| class TestHardcodedSubmodules(TestCase): | ||
| def test_hardcoded_stdlib_submodules_are_importable(self): | ||
| for parent_path, submodules in HARDCODED_SUBMODULES.items(): | ||
| for module_name in submodules: | ||
| path = f"{parent_path}.{module_name}" | ||
| with self.subTest(path=path): | ||
| # We can't use importlib.util.find_spec here, | ||
| # since some hardcoded submodules parents are | ||
| # not proper packages | ||
| importlib.import_module(path) | ||
| class TestPasteEvent(TestCase): | ||
| def prepare_reader(self, events): | ||
| console = FakeConsole(events) | ||
1 change: 1 addition & 0 deletions
1 Misc/NEWS.d/next/Core_and_Builtins/2025-08-30-17-15-05.gh-issue-69605.KjBk99.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fix some standard library submodules missing from the :term:`REPL` auto-completion of imports. |
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.