Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

IntroduceMetaPathFinder.discover andPathEntryFinder.discover #139899

Open
Labels
stdlibStandard Library Python modules in the Lib/ directorytopic-importlibtype-featureA feature request or enhancement
@FFY00

Description

@FFY00

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

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-importlibtype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2026 Movatter.jp