Mypy fails to correctly validate Type[T]
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
Dear all,
Consider the example code below:
from typing import Type, TypeVar, Tuple
T = TypeVar('T')
def func(a: T, a_type: Type[T]) -> Tuple[T, Type[T]]:
return a, a_type
a1, a1_type = func(1, int)
a2, a2_type = func(1, str)
reveal_type(a1)
reveal_type(a1_type)
reveal_type(a2)
reveal_type(a2_type)
The first call to func shouldn't give rise to any issues, while the second one should complain about an incompatible type in the a_type argument (as type(1) is not str).
Yet, whenever I run mypy (Python 3.7; mypy 0.770; MacOS 10.15.1) it fails to produce any errors, even going as far to infer the type of a2 as object instead of int.
>>> mypy test.py
test.py:13: note: Revealed type is 'builtins.int'
test.py:14: note: Revealed type is 'Type[builtins.int]'
test.py:15: note: Revealed type is 'builtins.object'
test.py:16: note: Revealed type is 'Type[builtins.object]'
Is anyone familiar with this issue?
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 eseguendo l’esempio fornito con mypy e confrontando i tipi e le diagnosi riportati con il risultato previsto. Traccia il percorso di inferenza dei tipi e di validazione degli argomenti per Type[T]; il lavoro è completato quando func(1, str) viene rifiutato, func(1, int) viene accettato e i tipi rivelati vengono inferiti correttamente.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 35/100