Spurious type error in `except` branch after an assignment from an `await` that throws
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
If a loop is guarded with an x is not None condition, and the body of the loop contains a try...except that assigns to x, mypy erroneously concludes that x might be None in the except branch.
To Reproduce
(https://mypy-play.net/?mypy=latest&python=3.12&gist=000b86de168d7bca72f3ff58de4121cb)
import asyncio
from collections.abc import Awaitable
from typing import Optional
R = Awaitable[Optional['R']]
async def boom() -> R:
raise ValueError("Boom!")
async def main() -> None:
x: Optional[R] = boom()
y: R
while x is not None:
try:
x = await x
except:
y = x
break
asyncio.run(main())
Expected Behavior
The program is well-typed.
Actual Behavior
main.py:17: error: Incompatible types in assignment (expression has type "R | None", variable has type "R") [assignment]
Found 1 error in 1 file (checked 1 source file)
Your Environment
See Playground details.
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 il riproduttore Python dell’esempio mypy-play collegato e traccia come viene gestito il restringimento dei tipi attraverso l’assegnazione await e il ramo except. L’issue non indica alcun file del repository né alcun test; il lavoro è completo quando l’esempio viene accettato senza l’errore di assegnazione segnalato e il comportamento ha una copertura di regressione.
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
- Abbastanza chiara
- Idoneità per principianti
- 35/100