Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print InspectError traceback in stubgen walk_packages when verbose is specified #18224

Merged
merged 1 commit into from
Dec 21, 2024

Conversation

gareth-cross
Copy link
Contributor

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:

mypy/mypy/stubgen.py

Lines 1522 to 1529 in 9405bfd

try:
result = find_module_path_and_all_py3(inspect, mod, verbose)
except CantImport as e:
tb = traceback.format_exc()
if verbose:
sys.stdout.write(tb)
if not quiet:
report_missing(mod, e.message, tb)

This comment has been minimized.

Copy link
Collaborator

@hamdanal hamdanal left a comment

Choose a reason for hiding this comment

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

Thanks. Could you print the traceback to stderr instead? You might also change find_module_paths_using_imports to print to stderr as well.

@gareth-cross gareth-cross force-pushed the verbose-import-error-print branch from 645f12f to 938757e Compare December 21, 2024 18:51
@gareth-cross
Copy link
Contributor Author

Thanks. Could you print the traceback to stderr instead? You might also change find_module_paths_using_imports to print to stderr as well.

Done for both.

@brianschubert
Copy link
Collaborator

(For future reference: try to avoid force pushing. It makes it hard to review changes. We squash merge anyway, so extra commits in the PR history doesn’t matter)

@gareth-cross
Copy link
Contributor Author

(For future reference: try to avoid force pushing. It makes it hard to review changes. We squash merge anyway, so extra commits in the PR history doesn’t matter)

Apologies - good call.

Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Copy link
Collaborator

@hamdanal hamdanal left a comment

Choose a reason for hiding this comment

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

Thanks.

@hauntsaninja hauntsaninja merged commit ceaf48d into python:master Dec 21, 2024
20 checks passed
@gareth-cross gareth-cross deleted the verbose-import-error-print branch December 21, 2024 23:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants