Skip to content

Commit e8fa54a

Browse files
refactor: improve suggestion message computation and punctuation handling
1 parent 509836b commit e8fa54a

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

Lib/traceback.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,10 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
10991099
self._is_syntax_error = True
11001100
self._exc_metadata = getattr(exc_value, "_metadata", None)
11011101
elif suggestion := _compute_suggestion_message(exc_type, exc_value, exc_traceback):
1102-
punctuation = "" if self._str.rstrip().endswith(('.', '!')) else "."
1102+
if self._str.endswith(('.', '?', '!', '...')):
1103+
punctuation = ''
1104+
else:
1105+
punctuation = '.'
11031106
self._str += f"{punctuation} {suggestion}"
11041107
else:
11051108
self.exc_type_qualname = None
@@ -1710,23 +1713,35 @@ def _compute_suggestion_error(exc_value, tb, wrong_name):
17101713
return suggestion
17111714

17121715

1713-
def _compute_suggestion_message(exc_type, exc_value, exc_traceback) -> str | None:
1714-
if issubclass(exc_type, ModuleNotFoundError) and \
1715-
sys.flags.no_site and \
1716-
getattr(exc_value, "name", None) not in sys.stdlib_module_names:
1717-
return ("Site initialization is disabled, did you forget to "
1718-
"add the site-packages directory to sys.path?")
1716+
def _compute_suggestion_message(exc_type, exc_value, exc_traceback):
1717+
if (
1718+
issubclass(exc_type, ModuleNotFoundError)
1719+
and sys.flags.no_site
1720+
and getattr(exc_value, "name", None) not in sys.stdlib_module_names
1721+
):
1722+
return ("Site initialization is disabled, did you forget to "
1723+
"add the site-packages directory to sys.path?")
17191724
if issubclass(exc_type, (ImportError, NameError, AttributeError)):
1720-
name_attribute = "name_from" if issubclass(exc_type, ImportError) else "name"
1721-
wrong_name = getattr(exc_value, name_attribute, None)
1725+
if issubclass(exc_type, ImportError):
1726+
wrong_name = getattr(exc_value, "name_from", None)
1727+
else:
1728+
wrong_name = getattr(exc_value, "name", None)
17221729
if wrong_name:
1723-
suggestion_error = _compute_suggestion_error(exc_value, exc_traceback, wrong_name)
1724-
suggestion_text = f"Did you mean: '{suggestion_error}'?" if suggestion_error else None
1725-
if issubclass(exc_type, NameError) and wrong_name in sys.stdlib_module_names:
1726-
if suggestion_error:
1727-
return f"{suggestion_text} Or did you forget to import '{wrong_name}'?"
1728-
return f"Did you forget to import '{wrong_name}'?"
1729-
return suggestion_text
1730+
other_name = _compute_suggestion_error(
1731+
exc_value, exc_traceback, wrong_name
1732+
)
1733+
maybe_builtin_import = (
1734+
issubclass(exc_type, NameError)
1735+
and wrong_name in sys.stdlib_module_names
1736+
)
1737+
if not other_name:
1738+
if maybe_builtin_import:
1739+
return f"Did you forget to import '{wrong_name}'?"
1740+
return None
1741+
text = f"Did you mean: '{other_name}'?"
1742+
if maybe_builtin_import:
1743+
return f"{text} Or did you forget to import '{wrong_name}'?"
1744+
return text
17301745
return None
17311746

17321747

0 commit comments

Comments
 (0)