python / python/mypy

stugen ignores and hides import errors despite not using `--ignore-errors`

Open
#16,762 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-stubgen
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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 https://github.com/python/mypy/issues/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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the stubgen CLI entry point and reproduce the -p and -m commands with a missing module, then compare them with --ignore-errors. Trace how import failures are reported and how the process exit status is chosen. Done means failed imports show their underlying error and return non-zero by default, while --ignore-errors preserves a zero status.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.