From 5ac39e145ebaa1b00961af448477e855125edbbf Mon Sep 17 00:00:00 2001 From: Locked-chess-official <13140752715@163.com> Date: Tue, 11 Nov 2025 14:19:59 +0800 Subject: [PATCH 1/5] clear builtins in suggestion when deleting --- Lib/traceback.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/traceback.py b/Lib/traceback.py index 9b4b8c7d566fe81..f7484b8f6e91483 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -11,6 +11,7 @@ import tokenize import io import _colorize +import dis from contextlib import suppress @@ -1677,8 +1678,9 @@ def _compute_suggestion_error(exc_value, tb, wrong_name): d = ( list(frame.f_locals) + list(frame.f_globals) - + list(frame.f_builtins) ) + if not dis.opname[frame.f_code.co_code[frame.f_lasti]].startswith('DELETE_'): + d += list(frame.f_builtins) d = [x for x in d if isinstance(x, str)] # Check first if we are in a method and the instance From d16cb294b92ee0266f6780f6e234bf9daef02587 Mon Sep 17 00:00:00 2001 From: Locked-chess-official <13140752715@163.com> Date: Tue, 11 Nov 2025 14:22:25 +0800 Subject: [PATCH 2/5] add News --- .../2025-11-11-14-21-33.gh-issue-138890.QwN0EU.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2025-11-11-14-21-33.gh-issue-138890.QwN0EU.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-11-11-14-21-33.gh-issue-138890.QwN0EU.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-11-11-14-21-33.gh-issue-138890.QwN0EU.rst new file mode 100644 index 000000000000000..2c8ff7b9ef869da --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-11-11-14-21-33.gh-issue-138890.QwN0EU.rst @@ -0,0 +1 @@ +Use a more simple way to forbidden the builtins name suggestion in deleting From e2e8386e0715fdbd048d25645b8b703a7d99bd33 Mon Sep 17 00:00:00 2001 From: Locked-chess-official <13140752715@163.com> Date: Thu, 9 Jul 2026 21:03:43 +0800 Subject: [PATCH 3/5] Update Lib/traceback.py --- Lib/traceback.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/traceback.py b/Lib/traceback.py index c631cb134a68d38..42a7f264b827558 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -1966,7 +1966,7 @@ def _compute_suggestion_error(exc_value, tb, wrong_name): list(frame.f_locals) + list(frame.f_globals) ) - if not dis.opname[frame.f_code.co_code[frame.f_lasti]].startswith('DELETE_'): + if not dis.opname[frame.f_code.co_code[frame.f_lasti]].startswith(('STORE_', 'DELETE_')): d += list(frame.f_builtins) d = [x for x in d if isinstance(x, str)] if not_normalized and wrong_name in d: From 47ab288d51485ad6867b69f16e8167abae3e19c8 Mon Sep 17 00:00:00 2001 From: Locked-chess-official <13140752715@163.com> Date: Thu, 9 Jul 2026 21:11:13 +0800 Subject: [PATCH 4/5] Remove repeat import of _colorize Removed unused import of _colorize module. --- Lib/traceback.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/traceback.py b/Lib/traceback.py index 42a7f264b827558..cbc7154fdb8fe65 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -14,7 +14,6 @@ import keyword import tokenize import io -import _colorize import dis import importlib.util import pathlib From 3006eef69750675d7e721863c26f7daaf1a2c2bc Mon Sep 17 00:00:00 2001 From: Locked-chess-official <13140752715@163.com> Date: Thu, 9 Jul 2026 23:24:57 +0800 Subject: [PATCH 5/5] add test --- Lib/test/test_traceback.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index e38d0942e463e9c..0e6c7b6eec18c8f 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -5109,6 +5109,26 @@ def func(): actual = self.get_suggestion(func) self.assertIn("forget to import '_io'", actual) + def test_name_error_do_not_suggest_builtin_name_in_delete(self): + def func(): + del next + + actual = self.get_suggestion(func) + self.assertNotIn("Did you mean: 'anext'", actual) + + def test_name_error_suggest_builtin_name_in_delete_if_not_delete_the_name_itself(self): + def func1(): + del nxt[1] + + actual = self.get_suggestion(func1) + self.assertIn("Did you mean: 'next'", actual) + + def func2(): + del nxt.__name__ + + actual = self.get_suggestion(func2) + self.assertIn("Did you mean: 'next'", actual) + class PurePythonSuggestionFormattingTests(