Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah now, I see what happens. I'm really sorry. Maybe the change in the finder is ok? cc @brettcannon

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what "finder" you're referring to as this it in the traceback module that's not a finder.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the lack of context. We use "finders" in sys.meta_path to know about the "known" modules via the discover() method but BuiltinImporter (I think it's this one) doesn't have a discover() method. Should we add one to this importer?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More precisely, we previously had this addition in the importer:

    @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:
            yield cls.find_spec(i)

for finder in sys.meta_path:
if discover := getattr(finder, 'discover', None):
d += [spec.name for spec in discover(parent)]
Expand Down
Loading