Wrong type hint after unpacking/swapping
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
Bug Report
Unpacking a tuple in the below scenario results in wrong assumed types.
To Reproduce
import typing as t
def spam(val: t.Union[t.Type, None] = None) -> None:
def eggs(type_: t.Type) -> None:
pass
reveal_type(val)
if isinstance(val, type):
reveal_type(val)
val, foo = None, val
reveal_type(val)
reveal_type(foo)
return eggs(foo)
Expected Behavior
After the line val, foo = None, val, I would expect that
valhas typeNone(ok)foohas typeType[Any]- the call to
eggs()passes
Actual Behavior
$ mypy test.py
test.py:9: note: Revealed type is 'Union[Type[Any], None]'
test.py:11: note: Revealed type is 'Type[Any]'
test.py:13: note: Revealed type is 'None'
test.py:14: note: Revealed type is 'None'
test.py:15: error: Argument 1 to "eggs" has incompatible type "None"; expected "Type[Any]"
Found 1 error in 1 file (checked 1 source file)
This does not match with my expectations 2 and 3.
Your Environment
Python 3.9
Mypy 0.790
MacOS 10.14.6
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 con mypy l'esempio Python segnalato e confronta i tipi rivelati prima e dopo val, foo = None, val. Analizza la gestione da parte del type-checker dell'unpacking di tuple e delle variabili il cui tipo è stato ristretto. Il lavoro è completato quando foo rimane Type[Any] e la chiamata a eggs(foo) passa senza errori.
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