mirror of
https://github.com/python/cpython
synced 2026-09-29 12:10:30 +03:00
gh-91058: Add error suggestions to 'import from' import errors (#98305)
This commit is contained in:
parent
1f737edb67
commit
7cfbb49fcd
11 changed files with 235 additions and 14 deletions
|
|
@ -707,9 +707,16 @@ class TracebackException:
|
|||
self.offset = exc_value.offset
|
||||
self.end_offset = exc_value.end_offset
|
||||
self.msg = exc_value.msg
|
||||
elif exc_type and issubclass(exc_type, ImportError) and \
|
||||
getattr(exc_value, "name_from", None) is not None:
|
||||
wrong_name = getattr(exc_value, "name_from", None)
|
||||
suggestion = _compute_suggestion_error(exc_value, exc_traceback, wrong_name)
|
||||
if suggestion:
|
||||
self._str += f". Did you mean: '{suggestion}'?"
|
||||
elif exc_type and issubclass(exc_type, (NameError, AttributeError)) and \
|
||||
getattr(exc_value, "name", None) is not None:
|
||||
suggestion = _compute_suggestion_error(exc_value, exc_traceback)
|
||||
wrong_name = getattr(exc_value, "name", None)
|
||||
suggestion = _compute_suggestion_error(exc_value, exc_traceback, wrong_name)
|
||||
if suggestion:
|
||||
self._str += f". Did you mean: '{suggestion}'?"
|
||||
if issubclass(exc_type, NameError):
|
||||
|
|
@ -1005,8 +1012,7 @@ def _substitution_cost(ch_a, ch_b):
|
|||
return _MOVE_COST
|
||||
|
||||
|
||||
def _compute_suggestion_error(exc_value, tb):
|
||||
wrong_name = getattr(exc_value, "name", None)
|
||||
def _compute_suggestion_error(exc_value, tb, wrong_name):
|
||||
if wrong_name is None or not isinstance(wrong_name, str):
|
||||
return None
|
||||
if isinstance(exc_value, AttributeError):
|
||||
|
|
@ -1015,6 +1021,12 @@ def _compute_suggestion_error(exc_value, tb):
|
|||
d = dir(obj)
|
||||
except Exception:
|
||||
return None
|
||||
elif isinstance(exc_value, ImportError):
|
||||
try:
|
||||
mod = __import__(exc_value.name)
|
||||
d = dir(mod)
|
||||
except Exception:
|
||||
return None
|
||||
else:
|
||||
assert isinstance(exc_value, NameError)
|
||||
# find most recent frame
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue