Keep in mind that this currently lives in a private module inside pyrepl (which is itself private) so the implementation is subject to breaking changes without notice.
However, if you really want to use it, this should do what you want:
>>> from _pyrepl._module_completer import ModuleCompleter
>>> c = ModuleCompleter()
>>> c.get_completions('import pa')
['pathlib']
I personaly find TAB mode more convenient to use (especially with laptops where you must hit a combination of keys to enter F8), but I totally understand the security concern behind it.
This is a really nice approach tbh. Iāve been playing with the demo and the TAB mode makes sense to me at least.
I agree with you that TAB mode is more practical than F8. Having to remember a special function key for this seems annoying especially on laptops where your hitting Fn+F8 or whatever. The double-tab pattern is way more intuitive and its already a habit for most people anyway.
From what i can tell the original concern was mostly about modules getting imported silently which could be slow or have side effects. With the confirmation step that concern goes away. If someone doesnt want to import they just dont hit tab again.
If safety / unintended usage are a concern, one could also go to a true double-tab operation, i.e. similar to a mouse double-click one would have to hit tab twice in short sequence. Hitting tab multiple times with delay would only show the warning but not trigger the import.
Possible positive side effect: we could leave out the message on immediate double-tab. That way experienced users are not bothered with the warning/notification anbout import.
Perhaps Iām too experienced, but sometimes hit tab three times quickly without much thought. (1 to complete a common suffix and 2 show the āmenuā of non-unique completions. _pyrepl only needs two tabs for this but my shell needs all three.)
Iām not convinced we need āsecurityā here, but if we do, I wouldnāt go with double-tab.
Yeah, F8 was a silly example of āanother keyā.
Shift+Tab maybe?
I brought up the security concern but I also think we should not overdo it, a second TAB is ok.
āTrue double-tabā as in double-clicking sounds awkward and unexpected to me (does this even exist in other UIs?), and letās not start with F8, triple-tabbing or other oddities.
All right, thanks all for the feedback! I think we can go with the second TAB then
Before I open a PR, I have a second interrogation: should we make effort to handle modules printing stuff during import gracefully? If you try to autocomplete from this import [1] in the demo, youāll see it goes quite messy, but maybe it is rare enough to not bother with it? (exceptions during import are already handled gracefully)
I suppose we could just temporarily catch stdout/stderr and dump their content properly on new lines before re-painting the prompt (that may be better in a follow-up PR, though?)
If anyone think this is not ok, please say so!
Otherwise you can check the new behavior on the WebAssembly demo(with a little hack, please read the note!)
Yeah, try āfrom this import <TAB>ā for some āunwanted side effectsā (or an easter egg, as you call it in the footnote)
And I notice that āfrom antigravity import <TAB>ā says āNo module named antigravityā, which may be true in pyodide, but isnāt true in the normal REPLā¦
Iām not sure this sort of weirdness is OK, TBH, even if it is rare.
Yes, this and antigravity are the two sidlib modules with side effects Iām aware of! But I cannot see why anyone would want to import something from them: these easter eggs are always explained as import this / import antigravity, which never trigger automatic import.
Note that antigravity is not suggested by the REPL (eg. if you type from anti<TAB>), precisely because it is not available the message youāre seeing is the same as if you try from spam import <TAB>.
I can understand this position, even if I thing the QOL benefits largely outweight the costs.
One thing I would like to do though is to capture any text printed on module import, display it cleanly below the prompt, and then the prompt agao, avoiding the ugly mess that from this import[1] currently is. Would that alleviate your concerns by some measure?
Agreed - Iād be fine with just special-casing those two, to be honest. (In the standard REPL, āfrom antigravity import ā¦ā would open the userās web browser, and that definitely shouldnāt happen, so special-casing is probably the only good option in practice, just to be safe).
If weāre just talking about stdlib modules, having a policy that they cannot have any side effects without some way to disable those side-effects in autocomplete mode[1] is probably better.
If weāre talking about 3rd party modules, output is the least of my concerns. But this has been discussed already, and if the consensus is that itās OK, then just because Iām personally still not comfortable with the risks, is no reason to rehash everything yet again.
Fine by me! I wrote a small script to try and import all stdlib modules / submodules and ran it (on Windows, masOS and WSL) to check for any side effects: in addition of antigravity and this, I found idlelib.idle and a few __main__ submodules that seem to lack a if __name__ == "__main__"' guard; Iām adding them all to a blacklist.