Skip to content

stugen ignores and hides import errors despite not using --ignore-errors #16762

Open
@bluenote10

Description

@bluenote10

Bug Report

The stubgen CLI currently ignores import errors even though it is not executed with --ignore-errors, setting a 0 return code despite broken usage.

To Reproduce

Simply via the CLI:

$ stubgen -p non_existing_package
# or
$ stubgen -m non_existing_module

Expected Behavior

Ideally stubgen should:

  • set a non-zero exit code to indicate that something went wrong.
  • show the output of what went wrong, i.e., the error that python -c "import ..." would show. See motivation below, why this is desired.

Only if executed with --ignore-errors the return code should be 0.

Actual Behavior

stubgen prints:

non_existing_package: Failed to import, skipping

and returns "successfully" with return code 0.

Your Environment

  • Mypy version used: 1.8.0
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: Python 3.10

Issue context

This issue looks like a regression, because it was already raised ~5 years ago in #6794 (even Guido van Rossum ran into it) and fixed in #6804.

Motivation for printing what went wrong

The motivation for setting a non-zero exit code in case of an error should be obvious.

The motivation for "showing what actually went wrong" is as follows: The case of stubgen -p non_existing_module is of course boring, because the reason what goes wrong in this case is obvious. But the issue can be more subtle. Imaging for instance a broken pybind11 library, for which we want to generate the stubs. Pybind11 performs certain sanity checks like name collisions at import time. These error information is crucial for actually fixing the problem. Doing python -c "import my_broken_native_module" shows the actual error cause like:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: generic_type: cannot initialize type "FooBar": an object with that name is already defined

Currently the stubgen swallows this key information, and just says Failed to import, skipping. This makes it tedious to analyze such an issue, because one has to manually re-run the import to see why it failed to import. From a UX perspective it would be much nicer to directly see what went wrong.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions