-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
gh-130472: Use fancycompleter in import completions #148188
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
Open
tomasr8
wants to merge
12
commits into
python:main
Choose a base branch
from
tomasr8:colorize-imports
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
89dde8f
Extract safe_getattr into a separate function
tomasr8 2b05c47
Extract colorize methods into reusable functions
tomasr8 8e75eb6
Colorize import completions
tomasr8 3b20cf3
Add tests
tomasr8 9e9d9d8
Simplify logic
tomasr8 a1576a0
Improve tests
tomasr8 7c95992
Restore original test name
tomasr8 f7dc27b
Use consistent type annotations
tomasr8 fe605cf
Attributes come first
tomasr8 30c576d
Make mypy happy
tomasr8 94e783c
Add a test
tomasr8 04b5d42
Add news entry
tomasr8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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
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 |
|---|---|---|
|
|
@@ -35,14 +35,15 @@ | |
| multiline_input, | ||
| code_to_events, | ||
| ) | ||
| from _colorize import ANSIColors, get_theme | ||
| from _pyrepl.console import Event | ||
| from _pyrepl.completing_reader import stripcolor | ||
| from _pyrepl._module_completer import ( | ||
| ImportParser, | ||
| ModuleCompleter, | ||
| HARDCODED_SUBMODULES, | ||
| ) | ||
| from _pyrepl.fancycompleter import Completer as FancyCompleter | ||
| from _pyrepl.fancycompleter import Completer as FancyCompleter, colorize_matches | ||
| import _pyrepl.readline as pyrepl_readline | ||
| from _pyrepl.readline import ( | ||
| ReadlineAlikeReader, | ||
|
|
@@ -1629,7 +1630,7 @@ def test_suggestions_and_messages(self) -> None: | |
| result = completer.get_completions(code) | ||
| self.assertEqual(result is None, expected is None) | ||
| if result: | ||
| compl, act = result | ||
| compl, _values, act = result | ||
| self.assertEqual(compl, expected[0]) | ||
| self.assertEqual(act is None, expected[1] is None) | ||
| if act: | ||
|
|
@@ -1641,6 +1642,39 @@ def test_suggestions_and_messages(self) -> None: | |
| new_imports = sys.modules.keys() - _imported | ||
| self.assertSetEqual(new_imports, expected_imports) | ||
|
|
||
| def test_colorize_import_completions(self) -> None: | ||
| theme = get_theme() | ||
| type_color = theme.fancycompleter.type | ||
| module_color = theme.fancycompleter.module | ||
| R = ANSIColors.RESET | ||
|
|
||
| colorize = lambda names, values: colorize_matches(names, values, theme) | ||
| config = ReadlineConfig(colorize_completions=colorize) | ||
| reader = ReadlineAlikeReader( | ||
| console=FakeConsole(events=[]), | ||
| config=config, | ||
| ) | ||
|
|
||
| # "from collections import de" -> defaultdict (type) and deque (type) | ||
| reader.buffer = list("from collections import de") | ||
| reader.pos = len(reader.buffer) | ||
| names, action = reader.get_module_completions() | ||
| self.assertEqual(names, [ | ||
| f"{type_color}defaultdict{R}", | ||
| f"{type_color}deque{R}", | ||
| ]) | ||
| self.assertIsNone(action) | ||
|
|
||
| # "from importlib.m" has submodule completions colored as modules | ||
| reader.buffer = list("from importlib.m") | ||
| reader.pos = len(reader.buffer) | ||
| names, action = reader.get_module_completions() | ||
| self.assertEqual(names, [ | ||
| f"{module_color}importlib.machinery{R}", | ||
| f"{module_color}importlib.metadata{R}", | ||
| ]) | ||
| self.assertIsNone(action) | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe a test with a name both an attribute + a submodule with the same name would be great, to be sure everything combines well?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added! |
||
|
|
||
| # Audit hook used to check for stdlib modules import side-effects | ||
| # Defined globally to avoid adding one hook per test run (refleak) | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would've find it more natural to return a
dict[str, Any]instead of two parallel lists, but I see that's what fancycompleter does, so let be it!But I wonder if we haven't passed the point where a namedtuple would be more readeable / maintainable? I hesitated when adding completion action, something like
the docstring could use an update either way 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I think this module is due for a small refactor tbh 😄 I'd leave it to another PR though