Skip to content

Commit

Permalink
Print InspectError traceback in stubgen walk_packages when verbose …
Browse files Browse the repository at this point in the history
…is specified (#18224)

This change modifies `walk_packages` such that the full `ImporError`
traceback is printed when a module cannot be imported. The goal is to
provide the user with more context to debug the error.

I implemented this change by mirroring existing behavior in
`find_module_paths_using_imports`:
https://github.com/python/mypy/blob/9405bfd9205ea369c11150907764fa46c03cb1f7/mypy/stubgen.py#L1522-L1529
  • Loading branch information
gareth-cross authored Dec 21, 2024
1 parent 924f818 commit ceaf48d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mypy/stubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1524,7 +1524,7 @@ def find_module_paths_using_imports(
except CantImport as e:
tb = traceback.format_exc()
if verbose:
sys.stdout.write(tb)
sys.stderr.write(tb)
if not quiet:
report_missing(mod, e.message, tb)
continue
Expand Down
4 changes: 4 additions & 0 deletions mypy/stubutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os.path
import re
import sys
import traceback
from abc import abstractmethod
from collections import defaultdict
from contextlib import contextmanager
Expand Down Expand Up @@ -70,6 +71,9 @@ def walk_packages(
try:
prop = inspect.get_package_properties(package_name)
except InspectError:
if verbose:
tb = traceback.format_exc()
sys.stderr.write(tb)
report_missing(package_name)
continue
yield prop.name
Expand Down

0 comments on commit ceaf48d

Please sign in to comment.