python / python/mypy

appending `#type:ignore` to an import does more than skipping analyzing the import

Offen
#14,778 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

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

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

Beginne mit dem Reproducer im Issue und führe mypy 0.990 aus, um zu bestätigen, dass die Unterdrückung des Imports auch den Fehler wegen der abstrakten Methode in Foo verbirgt. Verfolge die Verarbeitung von # type: ignore bei Imports und füge einen Regressionstest hinzu, der zeigt, dass der Import übersprungen wird, ohne Fehler in Code zu unterdrücken, der die importierte Basisklasse verwendet; fertig ist es, wenn das Beispiel die fehlende Implementierung von foo in Foo meldet.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers, devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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