GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-69605: PyREPL: insert "import" after "from foo <tab>" by loic-simon · Pull Request #148445 · python/cpython

Conversation

@loic-simon

@loic-simon loic-simon commented Apr 12, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Two import completion improvements:

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 match

It 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?)

  • Issue: Readline completion of module names in import statements #69605

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot mentioned this pull request Apr 12, 2026
Readline completion of module names in import statements #69605
Closed
@loic-simon
loic-simon marked this pull request as ready for review April 12, 2026 13:34
@loic-simon
loic-simon requested review from ambv, lysnikolaou and pablogsal as code owners April 12, 2026 13:34
@bedevere-app bedevere-app Bot added the awaiting review label Apr 12, 2026
tomasr8
tomasr8 reviewed Apr 12, 2026
View reviewed changes

@tomasr8 tomasr8 left a comment

Copy link
Copy Markdown
Member

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.

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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/test/test_pyrepl/test_pyrepl.py Outdated
("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 "),

@tomasr8 tomasr8 Apr 12, 2026

Copy link
Copy Markdown
Member

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'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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@loic-simon loic-simon Apr 12, 2026

Copy link
Copy Markdown
Contributor Author

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.

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:

  1. abandon the space insertion idea, and stick to "insert import" only
  2. keep it anyway (for all imports), but I think that can be quite frustating if you want to import from a submodule indeed
  3. don't insert space, but add a new special case: when the text to complete is an exact match, insert 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@tomasr8 tomasr8 Apr 13, 2026

Copy link
Copy Markdown
Member

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 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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@loic-simon loic-simon Apr 13, 2026

Copy link
Copy Markdown
Contributor Author

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.

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.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/_pyrepl/_module_completer.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@johnslavik
johnslavik self-requested a review April 12, 2026 20:30
@johnslavik

johnslavik commented Apr 12, 2026

Copy link
Copy Markdown
Member

I had the same idea a few days ago. Nice! I'll review soon.

loic-simon reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@loic-simon

loic-simon commented Apr 12, 2026

Copy link
Copy Markdown
Contributor Author

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).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tomasr8

tomasr8 commented Apr 13, 2026

Copy link
Copy Markdown
Member

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).

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?

loic-simon reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@loic-simon loic-simon mentioned this pull request Apr 15, 2026
gh-69605: fix PyREPL completions inserted for import statements with spaces #148629
Merged
@picnixz

picnixz commented Apr 21, 2026

Copy link
Copy Markdown
Member

You can have subpackages so from compression<TAB> could also show from compression.zstd. How should we handle this? Same for math.integer. I do not have a preference but I think we should not complete the import. We should stick to completing members IMO. Or is namespace completion triggered after a period only? (math.<TAB> should complete the namespace but math should complete toimport?)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@loic-simon

loic-simon commented Apr 21, 2026

Copy link
Copy Markdown
Contributor Author

Or is namespace completion triggered after a period only? (math.<TAB> should complete the namespace but math should complete toimport?)

That's the behavior currently proposed by this PR, yes. But I agree that if you're looking for compression.zstd, adding import after from compression<tab> is wrong and can be frustating.

So I guess the question is whether to drop this behavior entirely (eg. only insert import is there is already a space), or to check if there is potential submodules, and insert import only if there isn't.

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!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions

github-actions Bot commented Jul 25, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Jul 25, 2026
loic-simon added 2 commits October 20, 2026 23:22
@loic-simon @ambv
PyREPL completion: insert "import" after "from foo "
b0afaad
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)
@loic-simon @ambv
Insert "import" only when module name is complete
b1157ba
@ambv
ambv force-pushed the pyrepl-module-completion-insert-import branch from 4f24ade to b1157ba Compare October 20, 2026 21:23
@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Oct 20, 2026
ambv
ambv approved these changes Oct 21, 2026
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Oct 21, 2026
@ambv
Merge remote-tracking branch 'upstream/main' into pr-148445
8fba904
Co-authored-by: Muse Code (Meta Muse Spark)
@ambv
ambv merged commit fdee11f into python:main Oct 21, 2026
62 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@tomasr8 tomasr8 tomasr8 left review comments
@ambv ambv ambv approved these changes
@pablogsal pablogsal Awaiting requested review from pablogsal pablogsal is a code owner
@lysnikolaou lysnikolaou Awaiting requested review from lysnikolaou lysnikolaou is a code owner
@johnslavik johnslavik Awaiting requested review from johnslavik

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

stale Stale PR or inactive for long period of time. type-feature A feature request or enhancement

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@loic-simon @johnslavik @tomasr8 @picnixz @ambv
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.