-
-
Notifications
You must be signed in to change notification settings - Fork 34.1k
GH-145006: add ModuleNotFoundError hints when a module for a differen… #145007
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
FFY00
wants to merge
1
commit into
python:main
Choose a base branch
from
FFY00:gh-145006
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+44
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |||||
| import collections.abc | ||||||
| import itertools | ||||||
| import linecache | ||||||
| import os | ||||||
| import sys | ||||||
| import textwrap | ||||||
| import types | ||||||
|
|
@@ -1129,6 +1130,11 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None, | |||||
| self._str += (". Site initialization is disabled, did you forget to " | ||||||
| + "add the site-packages directory to sys.path " | ||||||
| + "or to enable your virtual environment?") | ||||||
| elif abi_tag := _find_incompatible_extension_module(module_name): | ||||||
| self._str += ( | ||||||
| ". Although a module with this name was found for a " | ||||||
| f"different Python version ({abi_tag})." | ||||||
| ) | ||||||
| else: | ||||||
| suggestion = _compute_suggestion_error(exc_value, exc_traceback, module_name) | ||||||
| if suggestion: | ||||||
|
|
@@ -1880,3 +1886,28 @@ def _levenshtein_distance(a, b, max_cost): | |||||
| # Everything in this row is too big, so bail early. | ||||||
| return max_cost + 1 | ||||||
| return result | ||||||
|
|
||||||
|
|
||||||
| def _find_incompatible_extension_module(module_name): | ||||||
| import importlib.machinery | ||||||
| import importlib.resources.readers | ||||||
|
|
||||||
| if not module_name or not importlib.machinery.EXTENSION_SUFFIXES: | ||||||
| return | ||||||
|
|
||||||
| # We assume the last extension is untagged (eg. .so, .pyd)! | ||||||
| # tests.test_traceback.MiscTest.test_find_incompatible_extension_modules | ||||||
| # tests that assumption. | ||||||
| untagged_suffix = importlib.machinery.EXTENSION_SUFFIXES[-1] | ||||||
|
|
||||||
| parent, _, child = module_name.rpartition('.') | ||||||
| if parent: | ||||||
| traversable = importlib.resources.files(parent) | ||||||
| else: | ||||||
| traversable = importlib.resources.readers.MultiplexedPath( | ||||||
| *filter(os.path.isdir, sys.path) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ) | ||||||
|
|
||||||
| for entry in traversable.iterdir(): | ||||||
| if entry.name.startswith(child + '.') and entry.name.endswith(untagged_suffix): | ||||||
| return entry.name.removeprefix(child + '.').removesuffix(untagged_suffix) | ||||||
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Library/2026-02-19-17-50-47.gh-issue-145006.9gqA0Q.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Add :exc:`ModuleNotFoundError` hints when a module for a different ABI | ||
| exists. |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.