Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork34.1k
Open
Description
Proposal:
In certain scenarios, such as implementing error suggestions forModuleNotFoundError, it would be helpful to have the ability to ask the finder to search for possible specs.
I propose adding a newoptionaldiscover method toMetaPathFinder andPathEntryFinder, as follows:
..method::discover(parent=None) An optional method which searches for possible specs with given *parent*. If *parent* is *None*,:meth:`MetaPathFinder.discover` will search for top-level modules. Returns an iterable of possible specs.
Python3.15.0a0free-threadingbuild (heads/main:744ec1d6c39,Oct102025,06:22:11) [GCC15.2.120250813]onlinuxType"help","copyright","credits"or"license"formoreinformation.>>>importsys>>>path_finder=sys.meta_path[-1]>>>importsysconfig>>>forspecinpath_finder.discover(sysconfig):...print(spec)...ModuleSpec(name='sysconfig.__init__',loader=<_frozen_importlib_external.SourceFileLoaderobjectat0x20000922c20>,origin='/home/anubis/git/cpython/Lib/sysconfig/__init__.py')ModuleSpec(name='sysconfig.__main__',loader=<_frozen_importlib_external.SourceFileLoaderobjectat0x20000922ca0>,origin='/home/anubis/git/cpython/Lib/sysconfig/__main__.py')>>>importurllib>>>forspecinpath_finder.discover(urllib):...print(spec)...ModuleSpec(name='urllib.robotparser',loader=<_frozen_importlib_external.SourceFileLoaderobjectat0x20000922220>,origin='/home/anubis/git/cpython/Lib/urllib/robotparser.py')ModuleSpec(name='urllib.response',loader=<_frozen_importlib_external.SourceFileLoaderobjectat0x20000920d20>,origin='/home/anubis/git/cpython/Lib/urllib/response.py')ModuleSpec(name='urllib.__init__',loader=<_frozen_importlib_external.SourceFileLoaderobjectat0x20000921ba0>,origin='/home/anubis/git/cpython/Lib/urllib/__init__.py')ModuleSpec(name='urllib.request',loader=<_frozen_importlib_external.SourceFileLoaderobjectat0x20000920520>,origin='/home/anubis/git/cpython/Lib/urllib/request.py')ModuleSpec(name='urllib.parse',loader=<_frozen_importlib_external.SourceFileLoaderobjectat0x20000921520>,origin='/home/anubis/git/cpython/Lib/urllib/parse.py')ModuleSpec(name='urllib.error',loader=<_frozen_importlib_external.SourceFileLoaderobjectat0x200009214a0>,origin='/home/anubis/git/cpython/Lib/urllib/error.py')
Possibly, we could also add a helper function that searchessys.meta_path, similarly toimportlib.util.find_spec. Although I don't think it is strictly necessary.
defdiscover(parent=None):forfinderinsys.meta_path:try:discover=getattr(finder,'discover')exceptAttributeError:continueyieldfromdiscover(parent)
This allows us to implement suggestions intraceback using the import system abstractions — seehttps://github.com/FFY00/cpython/tree/ModuleNotFoundError-suggestions.
Python3.15.0a0free-threadingbuild (heads/main:744ec1d6c39,Oct102025,06:22:11) [GCC15.2.120250813]onlinuxType"help","copyright","credits"or"license"formoreinformation.>>>importasdTraceback (mostrecentcalllast):File"<python-input-0>",line1,in<module>importasdModuleNotFoundError:Nomodulenamed'asd'.Didyoumean:'ast'?>>>importimportlib.utillTraceback (mostrecentcalllast):File"<python-input-1>",line1,in<module>importimportlib.utillModuleNotFoundError:Nomodulenamed'importlib.utill'.Didyoumean:'importlib.util'?
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response