python / python/mypy

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

Aperta
#16,762 0 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-stubgen
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Parti dal punto di ingresso della CLI stubgen e riproduci i comandi -p e -m con un modulo mancante, quindi confrontali con --ignore-errors. Traccia come vengono segnalati gli errori di importazione e come viene scelto lo stato di uscita del processo. Il lavoro è completo quando le importazioni non riuscite mostrano il relativo errore sottostante e restituiscono uno stato diverso da zero per impostazione predefinita, mentre --ignore-errors mantiene uno stato pari a zero.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
cli
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
55/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.