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
8 changes: 8 additions & 0 deletions Lib/importlib/_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you also add tests for that? and a documentation entry as well? or maybe let's ask @FFY00 if he wants to make this API documented (if not, we can rename the PR to "Include built-in modules in traceback suggestions")

if spec is not None: # assume that built-in modules have no submodule
return
for name in sys.builtin_module_names:
if mod_spec := cls.find_spec(name):
yield mod_spec

Comment on lines +946 to +948
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Are other discover() methods meant to return a list or an iterator


class FrozenImporter:

Expand Down
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
Loading