-
-
Notifications
You must be signed in to change notification settings - Fork 34.3k
gh-69605: Check for already imported modules in PyREPL module completion #139461
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
Merged
ambv
merged 24 commits into
python:main
from
loic-simon:pyrepl-module-completion-check-for-already-imported-modules
Jan 5, 2026
Merged
Changes from 21 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
6ed2776
PyREPL module completion: check for already imported modules
loic-simon 48fd43f
Add blurb
loic-simon 7ac428e
Better convey intent
loic-simon 6515e2f
[TEMP] debug tests on windows using modern technology (print statements)
loic-simon 7dbb906
[TEMP] More debugging, where is my module??
loic-simon ac3065a
[TEMP] More debugging, where is my module?? (bis)
loic-simon 75a33da
[TEMP] Day 57, deep into debugging, I still don't know where is my mo…
loic-simon ce124b1
[TEMP] Moar logs
loic-simon ee7047f
Merge branch 'main' into pyrepl-module-completion-check-for-already-i…
loic-simon 3f362cd
[TEMP] Is it a FileFinder cache issue??
loic-simon ed8ce73
[TEMP] Looks like a cache issue indeed
loic-simon 19c49bb
Tests: clean FileFinder cache
loic-simon 16e44af
Remove all debugging junk
loic-simon 14f6175
Small if refactor
loic-simon bdd7bdf
Merge branch 'pyrepl-module-completion-check-for-already-imported-mod…
loic-simon 78e4737
Full test coverage for new code
loic-simon e3f1ddb
Merge branch 'python:main' into pyrepl-module-completion-check-for-al…
loic-simon 2644400
Merge branch 'main' into pyrepl-module-completion-check-for-already-i…
tomasr8 5fa70cf
Merge branch 'main' into pyrepl-module-completion-check-for-already-i…
loic-simon d332e14
Check __spec__.has_location + refactor
loic-simon 1a5327c
Rename private helper
loic-simon d48f243
Simplify implementation
loic-simon e235e20
Fix find_spec call
loic-simon f6757fe
Remove unused import
loic-simon 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
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Core_and_Builtins/2025-09-30-21-59-56.gh-issue-69605.qcmGF3.rst
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 |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Fix edge-cases around already imported modules in the :term:`REPL` | ||
| auto-completion of imports. |
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.
This is not always going to be a real path right? e.g.
_frozen_importlib.. in any case it does not seem that useful to create the full path when we strip it away withos.path.dirnameright after.Perhaps we can have a smaller diff if instead of reverse-engineering the location, we simply look for the right
ModuleInfoin the global cache. After all if the module has already been imported, it should be visible topkgutil.iter_modules. Something like this:We can compare the module name and origin (maybe with something like
mod_info.module_finder.find_spec(mod_info.name)) to make sure we have the right one.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.
Clever! That will however provide no completions if the imported module exists, but is not the one returned by
pkgutil.iter_modulesanymore. But it's a quite degenerate case, and already half-supported because of theglobal_cachenot updating... So I think having no suggestions in this case is fine, and the diff is much smaller indeed! I was probably a bit over zealous 😅(frozen packages are also left out (
__phello__.__spec__.origin == 'frozen'vsmod_info.find_spec('__phello__').origin == '<venv>/Lib/__phello__/__init__.py'), but that's an even more niche issue -- I'm not ever sure there is frozen packages except__phello__!)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 considered it, but I think that's an edge case not worth trying to work around..
damn, that's unfortunate, we might need some special handling for frozen modules then. Note that there's also another special origin for built-in modules:
There are quite a bit. Here's an example of those that are imported at startup: