python / python/mypy

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

Aperta
#8,521 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug needs discussion priority-2-low topic-type-narrowing
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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)

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

Riproduci i due esempi di Callable con mypy 0.761 e Python 3.7.3, quindi confronta il percorso separato di dichiarazione e assegnazione con la forma combinata. Traccia la gestione di Callable nel type checker e aggiungi un test di regressione che si aspetti un errore di tipo dell’argomento incompatibile per il caso della dichiarazione separata.

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

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.