python / python/mypy

Callable type anntoation written apart of assignment causes false positive typecheck result.

Offen
#8,521 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug needs discussion priority-2-low topic-type-narrowing
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

A bugreport, mypy version is 0.761, python version is 3.7.3

This is a case where expected incapatible types, because i declared that foo's input type is float, and attempting to call it with str, but typechecking passed successful.
from typing import Callable

def bar(x):  return 5

foo : Callable[[float],int]
foo = bar

foo("test")

mypy this_file.py
Mypy's output:

Success: no issues found in 1 source file

A case with type annotation written with assignment wont give false positive result:
from typing import Callable

def bar(x):  return 5

foo : Callable[[float],int] = bar

foo("test")

mypy this_file.py
Mypy's output:

this_file.py:7: error: Argument 1 has incompatible type "str"; expected "float"
Found 1 error in 1 file (checked 1 source file)

I wasnt found other cases excluding cases with Callable type.

For example with Tuples type anntoation written apart of assignment all working fine:

from typing import Tuple

foo : Tuple[str,int]
foo = (1,3)

mypy this_file.py
Mypy's output:

this_file.py:4: error: Incompatible types in assignment (expression has type "Tuple[int, int]", variable has type "Tuple[str, int]")
Found 1 error in 1 file (checked 1 source file)

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

Reproduziere die beiden Callable-Beispiele mit mypy 0.761 und Python 3.7.3 und vergleiche anschließend den separaten Pfad für Deklaration und Zuweisung mit der kombinierten Form. Verfolge die Callable-Verarbeitung im Type Checker und füge einen Regressionstest hinzu, der für den Fall der separaten Deklaration einen Fehler wegen eines inkompatiblen Argumenttyps erwartet.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

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