Better error message for incompatible type assignment when using Generics
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
- Are you reporting a bug, or opening a feature request?
Feature Request
- Please insert below the code you are checking with mypy.
from typing import Generic, TypeVar
T = TypeVar("T", int, float)
class Foo(Generic[T]):
def __init__(self, val: T):
self.data: T = val
def bar(x: T) -> Foo[T]:
return Foo(x)
x = bar(1)
x = bar(2.0) # error thrown by mypy
- What is the actual behavior/output?
mypy throws this error:
error: Argument 1 to "bar" has incompatible type "float"; expected "int"
- What is the behavior/output you expect?
This error message is thrown because of the combination of assigning a different type to x and using Generic. The error message is unclear because it indicates a type error on the argument passed to bar and can be interpreted as bar not accepting arguments of type float.
Having a different error message that mentions "Incompatible types in assignment" would be clearer.
A message to make it clearer that expression has type Foo[float], variable has type Foo[int]
- What are the versions of mypy and Python you are using?
mypy 0.770 and Python 3.7.6
- What are the mypy flags you are using? (For example --strict-optional)
None
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 riproducendo l’esempio Generic con mypy 0.770 e Python 3.7.6, concentrandoti sull’assegnazione da Foo[int] a Foo[float]. Traccia la diagnostica prodotta per la chiamata a bar e l’assegnazione a x; il lavoro è completato quando il messaggio identifica chiaramente i tipi incompatibili della variabile e dell’espressione, invece di implicare che bar rifiuti argomenti float.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100