python / python/mypy

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

Offen
#16,762 0 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug topic-stubgen
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie am Einstiegspunkt der stubgen-CLI und reproduzieren Sie die Befehle -p und -m mit einem fehlenden Modul. Vergleichen Sie sie anschließend mit --ignore-errors. Verfolgen Sie, wie Importfehler gemeldet werden und wie der Exit-Status des Prozesses ausgewählt wird. Als abgeschlossen gilt die Aufgabe, wenn fehlgeschlagene Importe standardmäßig ihren zugrunde liegenden Fehler anzeigen und einen Status ungleich null zurückgeben, während --ignore-errors einen Status von null beibehält.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
cli
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
55/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.