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: Hardcode some stdlib submodules in PyREPL module completion (os.path, collections.abc...) by loic-simon · Pull Request #138268 · python/cpython

Conversation

@loic-simon

@loic-simon loic-simon commented Aug 30, 2025 •
edited by bedevere-app Bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

From #69605 (comment):

This is expected, collections.abc is not a real module, same for os.path so pkgutil won't find it. One thing we could do is hardcode these modules but I haven't had the time to look into it.

This PR adds a hardcoded dictionary of unusual standard library submodules that pkgutil.iter_modules can't find despite them being importable, so we can suggest them anyway.

The hardcoded dictionary comes from checking all documented modules to find which submodules _pyrepl._module_completer.ModuleCompleter was unable to detect.

I wrote a test to ensure all hardcoded modules are indeed importable, but I'm not sure we can programatically detect eventual new modules so we don't forget to add them, but it should be rare enough to not be too much a concern.

cc @tomasr8

  • 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
@loic-simon
PyREPL module completion: hardcode special stdlib submodules
dc13676
@loic-simon
loic-simon requested review from ambv, lysnikolaou and pablogsal as code owners August 30, 2025 17:11
@python-cla-bot

python-cla-bot Bot commented Aug 30, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-app bedevere-app Bot added the awaiting review label Aug 30, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Aug 30, 2025
Readline completion of module names in import statements #69605
Closed
@blurb-it
📜🤖 Added by blurb_it.
6f38649
AA-Turner
AA-Turner reviewed Aug 30, 2025
View reviewed changes
Comment thread Lib/_pyrepl/_module_completer.py Resolved

Uh oh!

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

AA-Turner
AA-Turner reviewed Aug 30, 2025
View reviewed changes
Comment thread Lib/_pyrepl/_module_completer.py Resolved

Uh oh!

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

loic-simon
loic-simon commented Aug 30, 2025
View reviewed changes
Comment thread Lib/test/test_pyrepl/test_pyrepl.py Resolved

Uh oh!

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

loic-simon added 2 commits August 30, 2025 19:26
@loic-simon
Review feedback
362e821
@loic-simon
Use .extend instead of +
a952c2a
aisk
aisk reviewed Aug 31, 2025
View reviewed changes
Comment thread Misc/NEWS.d/next/Core_and_Builtins/2025-08-30-17-15-05.gh-issue-69605.KjBk99.rst Resolved

Uh oh!

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

@tomasr8

tomasr8 commented Aug 31, 2025

Copy link
Copy Markdown
Member

Thanks! I was planning to implement this at some point, but you beat me to it :) I haven't checked too deeply, but we should make sure to only autocomplete these submodules when we are sure they are coming from the stdlib. We do not want to autocomplete them when a user intentionally shadows an stdlib module with their own because in that case they might not exist.

All reactions

Sorry, something went wrong.

Uh oh!

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

@loic-simon

loic-simon commented Aug 31, 2025

Copy link
Copy Markdown
Contributor Author

but we should make sure to only autocomplete these submodules when we are sure they are coming from the stdlib. We do not want to autocomplete them when a user intentionally shadows an stdlib module with their own because in that case they might not exist.

Yeah, I wondered about that, I'll check if we have a reliable and fast way to check this. Just looking at ModuleInfo.file_finder.path to see if it is relative to the stdlib path may do the trick!


A side note on this: when testing the current behavior by creating a folder collections with __init__.py and foo.py in it, the current mechanism suggest collections.foo, but is cannot be imported... because collections was already imported at startup (because of interactive mode):

% mkdir collections
% touch collections/__init__.py collections/foo.py
% python
Python 3.14.0rc2 [...]
>>> from collections import <TAB>
>>> from collections import foo
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    from collections import foo
ImportError: cannot import name 'foo' from 'collections' (/Users/loic/cpython/Lib/collections/__init__.py)
>>>
% python -c "from collections import foo; print(foo)"
<module 'collections.foo' from '/Users/loic/test/collections/foo.py'>

Not directly related, but it may be cool to check if the name to complete is already in sys.modules, and if yes only suggest imports from the location of the imported module.

All reactions

Sorry, something went wrong.

Uh oh!

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

loic-simon and others added 2 commits August 31, 2025 16:12
@loic-simon
Do not propose hardcoded submodules if local import
fbfe884
@ambv
Nit nittety nit
985bb20
@ambv
ambv merged commit 537133d into python:main Sep 15, 2025
@bedevere-app bedevere-app Bot removed the awaiting review label Sep 15, 2025
@ambv ambv added the needs backport to 3.14 bugs and security fixes label Sep 15, 2025
@miss-islington-app

miss-islington-app Bot commented Sep 15, 2025

Copy link
Copy Markdown

Thanks @loic-simon for the PR, and @ambv for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

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

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 15, 2025
@loic-simon @ambv @miss-islington
pythongh-69605: Hardcode some stdlib submodules in PyREPL module comp…
2239e63
…letion (os.path, collections.abc...) (pythonGH-138268)

(cherry picked from commit 537133d)

Co-authored-by: Loïc Simon <[email protected]>
Co-authored-by: Łukasz Langa <[email protected]>
@bedevere-app

bedevere-app Bot commented Sep 15, 2025

Copy link
Copy Markdown

GH-138943 is a backport of this pull request to the 3.14 branch.

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Sep 15, 2025
@loic-simon loic-simon mentioned this pull request Sep 30, 2025
gh-69605: Check for already imported modules in PyREPL module completion #139461
Merged
@loic-simon

loic-simon commented Sep 30, 2025

Copy link
Copy Markdown
Contributor Author

Not directly related, but it may be cool to check if the name to complete is already in sys.modules, and if yes only suggest imports from the location of the imported module.

Adressed in #139461.

All reactions

Sorry, something went wrong.

Uh oh!

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

ambv added a commit that referenced this pull request Oct 7, 2025
@miss-islington @loic-simon @ambv
[3.14] gh-69605: Hardcode some stdlib submodules in PyREPL module com…
d912e9a
…pletion (os.path, collections.abc...) (GH-138268) (GH-138943)

(cherry picked from commit 537133d)

Co-authored-by: Loïc Simon <[email protected]>
Co-authored-by: Łukasz Langa <[email protected]>
@loic-simon loic-simon mentioned this pull request Feb 13, 2026
gh-69605: Add math.integer to PyREPL module completer hardcoded list #144811
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@aisk aisk aisk left review comments
@AA-Turner AA-Turner AA-Turner left review comments
@pablogsal pablogsal Awaiting requested review from pablogsal pablogsal is a code owner
@lysnikolaou lysnikolaou Awaiting requested review from lysnikolaou lysnikolaou is a code owner
@ambv ambv Awaiting requested review from ambv ambv is a code owner

Uh oh!

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

Assignees

No one assigned

Labels

None yet

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 @tomasr8 @aisk @AA-Turner @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.