From c01925dce37479fdfb49c171ddc1d7850cb6a86f Mon Sep 17 00:00:00 2001 From: Locked-chess-official <13140752715@163.com> Date: Thu, 26 Mar 2026 00:06:10 +0800 Subject: [PATCH 1/5] just change traceback.py to make it --- Lib/traceback.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/traceback.py b/Lib/traceback.py index 1f9f151ebf5d39..bb57e812283efa 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -1755,7 +1755,7 @@ def _compute_suggestion_error(exc_value, tb, wrong_name): parent = importlib.util.find_spec(parent_name) else: parent = None - d = [] + d = list(sys.builtin_module_names) for finder in sys.meta_path: if discover := getattr(finder, 'discover', None): d += [spec.name for spec in discover(parent)] From b57a7e7b75048c5736418370f14749487a007dff Mon Sep 17 00:00:00 2001 From: Locked-chess-official <13140752715@163.com> Date: Thu, 26 Mar 2026 15:59:09 +0800 Subject: [PATCH 2/5] add test (why who added the suggestion for module name didn't add the traceback 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 5dc11253e0d5c8..b4ab39a869113a 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -5004,6 +5004,26 @@ def func(): actual = self.get_suggestion(func) self.assertIn("forget to import '_io'", actual) + def test_import_builtin_module_typo_suggestion(self): + def func(): + import itertool # noqa: F401 + + actual = self.get_suggestion(func) + self.assertIn("Did you mean: 'itertools'?", actual) + + def test_import_file_module_typo_suggestion(self): + def func(): + import abs # noqa: F401 + + actual = self.get_suggestion(func) + self.assertIn("Did you mean: 'abc'?", actual) + + def test_import_submodule_typo_suggestion(self): + def func(): + import multiprocessing.dumy # noqa: F401 + + actual = self.get_suggestion(func) + self.assertIn("Did you mean: 'multiprocessing.dummy'?", actual) class PurePythonSuggestionFormattingTests( From 03a92905f77b6c34695398af3173cb4d5a99bcec Mon Sep 17 00:00:00 2001 From: Locked-chess-official <13140752715@163.com> Date: Thu, 2 Apr 2026 19:47:27 +0800 Subject: [PATCH 3/5] Revert "just change traceback.py to make it" This reverts commit c01925dce37479fdfb49c171ddc1d7850cb6a86f. Changes to be committed: modified: 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 bb57e812283efa..1f9f151ebf5d39 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -1755,7 +1755,7 @@ def _compute_suggestion_error(exc_value, tb, wrong_name): parent = importlib.util.find_spec(parent_name) else: parent = None - d = list(sys.builtin_module_names) + d = [] for finder in sys.meta_path: if discover := getattr(finder, 'discover', None): d += [spec.name for spec in discover(parent)] From a06d51a138624491b6d55d320f1c0059e190a1cf Mon Sep 17 00:00:00 2001 From: Locked-chess-official <13140752715@163.com> Date: Thu, 2 Apr 2026 19:54:11 +0800 Subject: [PATCH 4/5] revert change in traceback and add discover --- Lib/importlib/_bootstrap.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index 45beb51659f5b7..eaf7d547b65efa 100644 --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -938,6 +938,14 @@ def is_package(cls, fullname): """Return False as built-in modules are never packages.""" return False + @classmethod + def discover(cls, spec=None): + if spec is not None: # assume that built-in modules have no submodule + return + for i in sys.builtin_module_names: + if i_spec := cls.find_spec(i): + yield i_spec + class FrozenImporter: From 16210183196fffad23917b3df6bde9e235e79957 Mon Sep 17 00:00:00 2001 From: Locked-chess-official <13140752715@163.com> Date: Thu, 2 Apr 2026 20:41:41 +0800 Subject: [PATCH 5/5] Update Lib/importlib/_bootstrap.py Co-authored-by: Victor Stinner --- Lib/importlib/_bootstrap.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index eaf7d547b65efa..3f4170f7b1cfdd 100644 --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -942,9 +942,9 @@ def is_package(cls, fullname): def discover(cls, spec=None): if spec is not None: # assume that built-in modules have no submodule return - for i in sys.builtin_module_names: - if i_spec := cls.find_spec(i): - yield i_spec + for name in sys.builtin_module_names: + if mod_spec := cls.find_spec(name): + yield mod_spec class FrozenImporter: