Skip to content

Conversation

loic-simon
Copy link
Contributor

@loic-simon loic-simon commented Aug 30, 2025

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

@python-cla-bot
Copy link

python-cla-bot bot commented Aug 30, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@tomasr8
Copy link
Member

tomasr8 commented Aug 31, 2025

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.

@loic-simon
Copy link
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.

@ambv ambv merged commit 537133d into python:main Sep 15, 2025
89 of 91 checks passed
@ambv ambv added the needs backport to 3.14 bugs and security fixes label Sep 15, 2025
@miss-islington-app
Copy link

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

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 15, 2025
…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
Copy link

bedevere-app bot commented Sep 15, 2025

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

@loic-simon
Copy link
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants