appending `#type:ignore` to an import does more than skipping analyzing the import
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug Report
I ran into relatively low priority unexpected behavior. I have this code
from anytree import NodeMixin # type:ignore
from typing import *
from abc import abstractmethod
class IFoo(Protocol):
@abstractmethod
def foo(self) -> None:
raise NotImplementedError()
class Foo(IFoo, NodeMixin):
...
def main() -> None:
a = Foo()
if __name__ == '__main__':
main()
anytree is a third-party package. When I run mypy on it, I expect that static analysis on anytree to be skipped, and static analysis on Foo to fail with something like "Foo needs to implement abstractmethod foo". What actually happens is that mypy exits with success saying 0 errors were found.
What I've tried to do to cause the expected behavior to occur was to use stubgen on the anytree install which generated stubs in the project folder, run mypy --install-types to install type hints for a dependency of anytree, and configure a mypy.ini in project root to point to the stubs. All of which was simple to do, but kind of tedious.
I also read https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports where it mentioned
This can result in mypy failing to warn you about errors in your code. Since operations on Any result in Any, these dynamic types can propagate through your code, making type checking less effective. See Dynamically typed code for more information.
So I guess the suppression of static checking is leaking from anytree.NodeMixin into Foo.
Actual Behavior
mypy exits with success, 0 errors
Your Environment
I'm on Windows.
- Mypy version used: 0.990
- Mypy command-line flags: N/A
- Mypy configuration options from
mypy.ini(and other config files): N/A - Python version used: 3.10.4
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con il riproduttore nell’issue ed esegui mypy 0.990 per confermare che la soppressione dell’import nasconde anche l’errore del metodo astratto in Foo. Traccia la gestione di # type: ignore sugli import e aggiungi un test di regressione che mostri che l’import viene saltato senza sopprimere gli errori nel codice che usa la classe base importata; il lavoro è terminato quando l’esempio segnala l’implementazione mancante di foo in Foo.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers, devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100