GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — gh-69605: PyREPL: insert "import" after "from foo <tab>" by loic-simon · Pull Request #148445 · python/cpython Conversation
|
| Input | Current | Proposed |
|---|---|---|
from math <tab> |
from math |
from math import |
from mat<tab> |
from math |
from math (extra space) |
The first one should be pretty uncontroversial, since import is the only valid syntax here.
The second only change the behavior when a single match is found:
from cont<tab> still inserts from context (common prefix to contextlib and contextvar)from foo<tab> still inserts nothing, since we have no matchIt allows from mat<tab><tab> to insert from math import , which I find really smooth and pleasant to use!
Implementation notes: I settled to extend ImportParser.parse to also return a space_end boolean, and use that in ModuleCompleter.complete to return an import suggestion / add a space when needed.
This is a little more generic that needed for this change (eg. we don't really care abuout the space in import foo ), but it keep the parsing logic and completion logic orthogonal.
cc @tomasr8 (it'll have a few conflicts with your coloring PR, but I have no way to point this PR to some virtual "after your PR is merged" branch, have I?)
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the quick PR!
It allows from mat to insert from math import , which I find really smooth and pleasant to use!
This is quite nice indeed!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| ("from importlib.res\t\n", "from importlib.resources"), | ||
| ("from importlib.\t\tres\t\n", "from importlib.resources"), | ||
| ("from importlib.resources.ab\t\n", "from importlib.resources.abc"), | ||
| ("from impo\t\n", "from importlib "), |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure whether we should change this, what do you think? importlib has submodules so I think we should not add the final space, in case you want to type something like from importlib.foo ...
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Hmm yes, I didn't thought of that! But that applies to a lot of modules (including my example of from mat import <tab><tab>)...
I think having a different behaviour if the module has submodules or not would be both obscure and hard to implement.
So I see two three paths forward:
import.eg. to come back to my table:
| Input | Current | Proposed |
|---|---|---|
from math <tab> |
from math |
from math import |
from mat<tab> |
from math |
from math (no more extra space) |
from math<tab> |
from math |
from math import (insert space + import) |
That allow from mat<tab><tab> to still work, and should be quite straightforward, so at a glance I quite like it!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I think having a different behaviour if the module has submodules or not would be both obscure and hard to implement.
Hard probably, but UX-wise I think it'd be nice if it added the space for modules that don't have submodules. It would save you one keystroke and immediately signal that there are no submodules to import from.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Okay, I implemented the point 3 above, I tend to think it achieves the same smooth behavior (double tab to insert import) and is more simple to reason about.
But I'm eager to hear what you think, and happy to try and implement the "no submodules case" solution if we think it's better!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
I had the same idea a few days ago. Nice! I'll review soon. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Note: I juste found an existing bug (on main) while testing things 😅 >>> from math .<tab> >>> from math .ath.integer # instead of `from math .integer` (valid syntax!) It's quite related to this change so I'm inclined to fix it in it's PR, if it's ok (and doesn't turn out too complicated). |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Yeah the parser assumes what you type is at least somewhat valid, but we can fix it if it's not too complicated. I would consider this a bug so we should backport it, could you make a separate PR for it? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
You can have subpackages so |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
That's the behavior currently proposed by this PR, yes. But I agree that if you're looking for So I guess the question is whether to drop this behavior entirely (eg. only insert I'm leaning slightly toward the first option, because I think the second is less obvious and can be complicated to understand as a user, but I'm happy to try and implement it if we think it's worth it! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
This PR is stale because it has been open for 90 days with no activity. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Improve _pyrepl._module_completer.ModuleCompleter.get_completions:
* "from x.y.z " [tab]-> "from x.y.z import "
* "from foo" [tab]-> "from foo " [tab]-> "from foo import"
(if only one suggestion)
4f24ade to
b1157ba
Compare
Co-authored-by: Muse Code (Meta Muse Spark)
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.