Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 33.9k
gh-69605: Add module autocomplete to PyREPL#129329
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
Uh oh!
There was an error while loading. Please reload this page.
Conversation
tomasr8 commented Jan 27, 2025 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
picnixz commented Jan 27, 2025
I have a branch which adds a filtering logic to the completer (see https://discuss.python.org/t/repl-introduce-generic-filters-to-filter-auto-completion-matches/77553) and I wondered whether we could expand the Completer interface in order to have a more generic hook category. Both your work and mine can be orthogonal but we might take this opportunity to make the completer class more generic (I haven't looked at the PR in details as I'm travelling now) |
tomasr8 commented Jan 27, 2025
Right, I remember seeing your dpo post before! I think it's a good idea :) With this PR I decided to target PyREPL since it is less stable than rlcompleter and thus it's easier to expand/make changes. Though we could definitely think about unifying the interface with that of rlcompleter. For now the API is a bit ad hoc so I'd welcome some discussion in that direction 🙂 |
Lib/_pyrepl/readline.py Outdated
| returnresult | ||
| defget_module_completions(self) ->list[str]: | ||
| completer=ModuleCompleter(namespace={'__package__': '_pyrepl'}) # TODO: namespace? |
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.
One thing I wasn't sure about is how to handle relative imports in regards to __package__. In PyREPL, it is set to _pyrepl so I replicate that here but maybe we want it to be configurable?
hugovk commented Jan 30, 2025
>>> import re [ complete but not unique ]
>>> import re re reprlib readline resource requests
>>> import re KeyboardInterrupt readline resource requests >>> re reprlib readline resource requestsI'd expect something more like:
>>> import re KeyboardInterrupt readline resource requests >>> KeyboardInterrupt readline resource requests >>> KeyboardInterrupt readline resource requests >>> re reprlib readline resource requestsI'd expect something more like: >>> import re KeyboardInterrupt >>> KeyboardInterrupt >>> KeyboardInterrupt >>> |
tomasr8 commented Jan 30, 2025
Answering here what I wrote on Discord - the |
gaogaotiantian commented Feb 25, 2025
I only did a quick look at the code. Are you trying to import the module during completion? |
tomasr8 commented Feb 25, 2025
Yes I try to import it to ensure it's a valid package and so that I can easily list the submodules (e.g. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
gaogaotiantian commented Feb 25, 2025
I think so. Implicitly import a module during auto-completion seems a bit unintentional to me. For example, importing |
tomasr8 commented Feb 25, 2025
This PR uses I do import in case you type What I think I cannot avoid is an import in case you type |
gaogaotiantian commented Feb 25, 2025
Personally, I would prefer if the auto-completer never imports any module without my knowledge, even if that means it can't do its job in some cases. It's okay if it completes when the module already imported, but importing a new module during auto-completion seems wrong to me. I think we should at least get opinions from a few other core devs about this, because in my mind this is a relatively large new behavior. |
tomasr8 commented Feb 25, 2025
I admit I do not fully understand the implications of this so I think getting more opinions is a good idea :) Do you want me to start a discussion on Discord? |
gaogaotiantian commented Feb 25, 2025
Discord or/and dpo maybe? We should at least get some opinions from repl owners. |
tomasr8 commented Feb 25, 2025
ok! It's getting a bit late here, so I'll do it tomorrow 🙂 |
tomasr8 commented Mar 10, 2025
I updated the PR following the discussion on DPO: https://discuss.python.org/t/looking-for-feedback-on-adding-import-autocomplete-to-pyrepl/82281
|
Lib/_pyrepl/readline.py Outdated
| returnresult | ||
| defget_module_completions(self) ->list[str]: | ||
| completer=ModuleCompleter(namespace={'__package__': '_pyrepl'}) # TODO: namespace? |
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.
Either remove the TODO or we should handle this differently
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 removed the TODO and left a comment. Inside pyrepl, __package__ is set to _pyrepl so the module completer should use the same value when resolving relative packages.
Lib/_pyrepl/readline.py Outdated
| defget_module_completions(self) ->list[str]: | ||
| completer=ModuleCompleter(namespace={'__package__': '_pyrepl'}) # TODO: namespace? | ||
| line=self.get_line() | ||
| returncompleter.get_completions(line) |
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.
Can this raise? What do we want to do if any of the pkgutil raises for any reason?
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.
It shouldn't unless iter_modules or find_spec raise. Since those can call code from user-supplied finders, they can raise an arbitrary exception. I could add a simple except Exception block and simply return no completions if that happens. To the user it would look like no completions are available rather than outright crashing the repl.
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.
Added in fd81999
Lib/_pyrepl/readline.py Outdated
| """Global module cache""" | ||
| ifnotself._global_cacheorself._curr_sys_path!=sys.path: | ||
| self._curr_sys_path=sys.path[:] | ||
| self._global_cache=list(pkgutil.iter_modules()) |
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.
Nothing to do here but I am a bit concerned about how this can perform for a lot of packages
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.
See this #129329 (comment) with timings
Lib/_pyrepl/readline.py Outdated
| ifself.code.rstrip().endswith('import') andself.code.endswith(' '): | ||
| returnResult(from_name=self.parse_empty_from_import(), name='') | ||
| ifself.code.rstrip().endswith('from') andself.code.endswith(' '): | ||
| returnResult(from_name='') |
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.
This calls code.rstrip() a lot, maybe is worth setting that as an attribute or just always use self.code = code.rstrip(). What do you think?
At the very least maybe set it as a local so you don't strip twice
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 added it as a local since it's only called in 3 places
Lib/_pyrepl/readline.py Outdated
| returnresult | ||
| defget_module_completions(self) ->list[str]: | ||
| completer=ModuleCompleter(namespace={'__package__': '_pyrepl'}) # TODO: namespace? |
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.
Is this creating one of this guys per request? Should we cache the ModuleCompleter?
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.
it is, and yes we should cache it. Updated in 5c11124 (I also moved the completer to a separate file to avoid having to reorder everything when I added it to the ReadlineConfig)
tomasr8Apr 19, 2025 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
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.
Also changed in f4e290a so that each Reader gets a new ModuleCompleter instance (with its own module cache)
pablogsal commented Apr 19, 2025
@tomasr8 can you try to play a bit with some edge cases (tons of packages, etc) to see how the performance is for extreme circumstances? I want to be aware of the ways this can "break" |
tomasr8 commented Apr 19, 2025 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
Thanks for taking the time to review this, I really appreciate it!
When it comes to performance, the biggest bottleneck is the call to Here are some timings for lots of packages. I installed the top 1000 pypi packages (at least those that can be installed on 3.14 so 836 packages installed): ~ pip freeze | wc -l 836The timings are on a laptop with Intel ultra 9 185H CPU and a new-ish SSD (relevant because For those ~800 packages in a single location, >>>importtime, pkgutil>>>start=time.time(); list(pkgutil.iter_modules()); time.time() -start0.03072071075439453Typing >>>import<tab>0.03162884712219238Subsequent completion requests are much faster since the results are cached: >>>import<tab>0.002469778060913086Another thing I tried is multiple search locations (i.e. multiple 5 different locations and ~4000 packages total: `pkgutil.iter_modules`: 0.3535459041595459sInitial`import <tab>`: 0.3592982292175293sSubsequent`import <tab>`: 0.0021576881408691406s10 different locations and ~8000 packages total: `pkgutil.iter_modules`: 0.6836235523223877sInitial`import <tab>`: 0.6652779579162598sSubsequent`import <tab>`: 0.0002522468566894531sThe initial search is about 0.35s and 0.68s respectively, while the subsequent ones are again very cheap. |
hugovk commented Apr 23, 2025
I still get this failure locally: ====================================================================== FAIL: test_import_completions (test.test_pyrepl.test_pyrepl.TestPyReplModuleCompleter.test_import_completions) (code='import path\t\n') ---------------------------------------------------------------------- Traceback (most recent call last): File "/Users/hugo/github/python/cpython/main/Lib/test/test_pyrepl/test_pyrepl.py", line 938, in test_import_completionsself.assertEqual(output, expected) ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^AssertionError: 'import path' != 'import pathlib' - import path + import pathlib ? +++It's because my env isn't a clean slate because I've pip installed some packages for testing on 3.14, and a dependency is interfering: Python 3.14.0a7+ (heads/completer:f4e290a03f1, Apr 23 2025, 13:32:14) [Clang 16.0.0 (clang-1600.0.26.6)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import path<tab> pathlib pathvalidate❯ ./python.exe -m pip freeze | grep pathpathvalidate==3.2.0I don't think this needs to block initial merge before the freeze, but would be good to fix. (PS there's a merge conflict on some imports.) |
tomasr8 commented Apr 23, 2025
Weird, I tested this locally with some scripts on |
tomasr8 commented Apr 23, 2025
Ok so |
hugovk commented Apr 24, 2025
Passing now, thanks! |
c3a7118 into python:mainUh oh!
There was an error while loading. Please reload this page.
pablogsal commented Apr 25, 2025
LGTM, fantastic job @tomasr8 |
DPO discussion thread: https://discuss.python.org/t/looking-for-feedback-on-adding-import-autocomplete-to-pyrepl/82281
Adds a module autocomplete functionality to the PyREPL. Some examples first:
(Check the tests to see a complete list of what is and is not supported)
The implementation is based on the original patch by @vadmium and adapted to PyREPL.
It can autcomplete both
importandfrom ... importstatements as long as the import fragment is valid. It uses a tokenizer/parser-based approach so that it can correctly recognize more complex contructs such asfrom foo import (bar as baz, qux<tab>.Module search is done by
pkgutil.iter_modules.I wasn't sure where exactly to put this - for now it hooks into
ReadlineAlikeReader.get_completions, let me know if there's a better place for it!Note that, if there are any import completions available, normal completions are turned off (so that e.g.
import m<tab>will not suggestimport map().Feedback welcome :)