Title: Introduce `MetaPathFinder.discover` and `PathEntryFinder.discover` · Issue #139899 · python/cpython · GitHub
Open Graph Title: Introduce `MetaPathFinder.discover` and `PathEntryFinder.discover` · Issue #139899 · python/cpython
X Title: Introduce `MetaPathFinder.discover` and `PathEntryFinder.discover` · Issue #139899 · python/cpython
Description: Proposal: In certain scenarios, such as implementing error suggestions for ModuleNotFoundError, it would be helpful to have the ability to ask the finder to search for possible specs. I propose adding a new optional discover method to Me...
Open Graph Description: Proposal: In certain scenarios, such as implementing error suggestions for ModuleNotFoundError, it would be helpful to have the ability to ask the finder to search for possible specs. I propose add...
X Description: Proposal: In certain scenarios, such as implementing error suggestions for ModuleNotFoundError, it would be helpful to have the ability to ask the finder to search for possible specs. I propose add...
Opengraph URL: https://github.com/python/cpython/issues/139899
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Introduce `MetaPathFinder.discover` and `PathEntryFinder.discover`","articleBody":"### Proposal:\n\nIn certain scenarios, such as implementing error suggestions for `ModuleNotFoundError`, it would be helpful to have the ability to ask the finder to search for possible specs.\n\nI propose adding a new **optional** `discover` method to `MetaPathFinder` and `PathEntryFinder`, as follows:\n\n```rst\n .. method:: discover(parent=None)\n\n An optional method which searches for possible specs with given *parent*.\n If *parent* is *None*, :meth:`MetaPathFinder.discover` will search for\n top-level modules.\n\n Returns an iterable of possible specs.\n```\n\n```python\nPython 3.15.0a0 free-threading build (heads/main:744ec1d6c39, Oct 10 2025, 06:22:11) [GCC 15.2.1 20250813] on linux\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n>>> import sys\n>>> path_finder = sys.meta_path[-1]\n>>> import sysconfig\n>>> for spec in path_finder.discover(sysconfig):\n... print(spec)\n...\nModuleSpec(name='sysconfig.__init__', loader=\u003c_frozen_importlib_external.SourceFileLoader object at 0x20000922c20>, origin='/home/anubis/git/cpython/Lib/sysconfig/__init__.py')\nModuleSpec(name='sysconfig.__main__', loader=\u003c_frozen_importlib_external.SourceFileLoader object at 0x20000922ca0>, origin='/home/anubis/git/cpython/Lib/sysconfig/__main__.py')\n>>> import urllib\n>>> for spec in path_finder.discover(urllib):\n... print(spec)\n...\nModuleSpec(name='urllib.robotparser', loader=\u003c_frozen_importlib_external.SourceFileLoader object at 0x20000922220>, origin='/home/anubis/git/cpython/Lib/urllib/robotparser.py')\nModuleSpec(name='urllib.response', loader=\u003c_frozen_importlib_external.SourceFileLoader object at 0x20000920d20>, origin='/home/anubis/git/cpython/Lib/urllib/response.py')\nModuleSpec(name='urllib.__init__', loader=\u003c_frozen_importlib_external.SourceFileLoader object at 0x20000921ba0>, origin='/home/anubis/git/cpython/Lib/urllib/__init__.py')\nModuleSpec(name='urllib.request', loader=\u003c_frozen_importlib_external.SourceFileLoader object at 0x20000920520>, origin='/home/anubis/git/cpython/Lib/urllib/request.py')\nModuleSpec(name='urllib.parse', loader=\u003c_frozen_importlib_external.SourceFileLoader object at 0x20000921520>, origin='/home/anubis/git/cpython/Lib/urllib/parse.py')\nModuleSpec(name='urllib.error', loader=\u003c_frozen_importlib_external.SourceFileLoader object at 0x200009214a0>, origin='/home/anubis/git/cpython/Lib/urllib/error.py')\n```\n\nPossibly, we could also add a helper function that searches `sys.meta_path`, similarly to `importlib.util.find_spec`. Although I don't think it is strictly necessary.\n\n```python\ndef discover(parent=None):\n for finder in sys.meta_path:\n try:\n discover = getattr(finder, 'discover')\n except AttributeError:\n continue\n yield from discover(parent)\n```\n\n---\n\nThis allows us to implement suggestions in `traceback` using the import system abstractions — see https://github.com/FFY00/cpython/tree/ModuleNotFoundError-suggestions.\n\n```python\nPython 3.15.0a0 free-threading build (heads/main:744ec1d6c39, Oct 10 2025, 06:22:11) [GCC 15.2.1 20250813] on linux\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n>>> import asd\nTraceback (most recent call last):\n File \"\u003cpython-input-0>\", line 1, in \u003cmodule>\n import asd\nModuleNotFoundError: No module named 'asd'. Did you mean: 'ast'?\n>>> import importlib.utill\nTraceback (most recent call last):\n File \"\u003cpython-input-1>\", line 1, in \u003cmodule>\n import importlib.utill\nModuleNotFoundError: No module named 'importlib.utill'. Did you mean: 'importlib.util'?\n```\n\n### Has this already been discussed elsewhere?\n\nNo response given\n\n### Links to previous discussion of this feature:\n\n_No response_\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-139900\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/FFY00","@type":"Person","name":"FFY00"},"datePublished":"2025-10-10T12:58:01Z","url":"https://github.com/python/cpython/issues/139899"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:6660520f-439b-4127-1ffb-05d73025fd69 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EBD0:296654:361144F:4803368:6B020975 |
| html-safe-nonce | a245c4a066d995b871b7f7c35471326fcd9e3e3b1121246b466bf163c03efc6f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQkQwOjI5NjY1NDozNjExNDRGOjQ4MDMzNjg6NkIwMjA5NzUiLCJ2aXNpdG9yX2lkIjoiNTA5MTgyMzk1NzU5MDU0MjcxMCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 89999bcdd43ea7a7a4ac126a2eefe640d11ed1715c79414c12294aa436ddd47d |
| hovercard-subject-tag | issue:3502799334 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/139899/issue_layout |
| twitter:image | https://opengraph.githubassets.com/88d708f30a1e0c3d83f8de6e1c7f7cb2fe02cc332b5b0620492a1c4ff951981a/python/cpython/issues/139899 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/88d708f30a1e0c3d83f8de6e1c7f7cb2fe02cc332b5b0620492a1c4ff951981a/python/cpython/issues/139899 |
| og:image:alt | Proposal: In certain scenarios, such as implementing error suggestions for ModuleNotFoundError, it would be helpful to have the ability to ask the finder to search for possible specs. I propose add... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | FFY00 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width