summaryrefslogtreecommitdiffstats
path: root/Lib/traceback.py
diff options
context:
space:
mode:
authorPablo Galindo Salgado <Pablogsal@gmail.com>2022-10-15 22:13:33 (GMT)
committerGitHub <noreply@github.com>2022-10-15 22:13:33 (GMT)
commitbb56dead336357153a0c3b8cc9d9d6856d2c5a03 (patch)
treeb3e336da20cd08a82d86f19c054f8c780ee422da /Lib/traceback.py
parent3a639bbeace73d54f7e5431d3224c8c8223d81ae (diff)
downloadcpython-bb56dead336357153a0c3b8cc9d9d6856d2c5a03.zip
cpython-bb56dead336357153a0c3b8cc9d9d6856d2c5a03.tar.gz
cpython-bb56dead336357153a0c3b8cc9d9d6856d2c5a03.tar.bz2
gh-98254: Include stdlib module names in error messages for NameErrors (#98255)
Diffstat (limited to 'Lib/traceback.py')
-rw-r--r--Lib/traceback.py7
1 files changed, 7 insertions, 0 deletions
diff --git a/Lib/traceback.py b/Lib/traceback.py
index c46ddaf..bb7856a 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -712,6 +712,13 @@ class TracebackException:
suggestion = _compute_suggestion_error(exc_value, exc_traceback)
if suggestion:
self._str += f". Did you mean: '{suggestion}'?"
+ if issubclass(exc_type, NameError):
+ wrong_name = getattr(exc_value, "name", None)
+ if wrong_name is not None and wrong_name in sys.stdlib_module_names:
+ if suggestion:
+ self._str += f" Or did you forget to import '{wrong_name}'"
+ else:
+ self._str += f". Did you forget to import '{wrong_name}'"
if lookup_lines:
self._load_lines()
self.__suppress_context__ = \