python / python/mypy

attribute failure when unpacking from tuple and wait_for

Aperta
#15,569 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

The following snippet:

from dataclasses import dataclass
from asyncio import wait_for

@dataclass
class A:
    i: int | None = None

async def bar()->tuple[int, int]:
    return (0,0)

async def foo()->int:
    a = A()
    a.i, _0 = await wait_for(bar(), timeout=5)
    return a.i

fails with the error: error: Incompatible return value type (got "int | None", expected "int") [return-value]

note that this snippet works (where bar returns a straight value):

from dataclasses import dataclass
from asyncio import wait_for

@dataclass
class A:
    i: int | None = None

async def bar()->int:
    return 0

async def foo()->int:
    a = A()
    a.i = await wait_for(bar(), timeout=5)
    return a.i

as does this (without wait_for):

from dataclasses import dataclass
from asyncio import wait_for

@dataclass
class A:
    i: int | None = None

async def bar()->tuple[int, int]:
    return (0,0)

async def foo()->int:
    a = A()
    a.i, _0 = await bar()
    return a.i

or this (where bar returns a list):

from dataclasses import dataclass
from asyncio import wait_for

@dataclass
class A:
    i: int | None = None

async def bar()->list[int]:
    return [0,0]

async def foo()->int:
    a = A()
    a.i, _0 = await wait_for(bar(), timeout=5)
    return a.i

or this (setting a variable instead of attribute):

from asyncio import wait_for

async def bar()->tuple[int, int]:
    return (0,0)

async def foo()->int:
    a_i = None
    a_i, _0 = await wait_for(bar(), timeout=5)
    return a_i

To Reproduce
run https://mypy-play.net/?mypy=latest&python=3.11&flags=local-partial-types&gist=5046bb7c065a2594953c9e876bdfd29e

Expected Behavior
the snippet should pass just as though the call was made without wait_for

Actual Behavior

the snippet fails with:

main.py:14: error: Incompatible return value type (got "int | None", expected "int")  [return-value]
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

Inizia eseguendo la riproduzione collegata di mypy-play e confronta le varianti tuple, list, direct-call e variable-assignment mostrate nel report. Traccia il modo in cui await wait_for con tuple unpacking gestisce l’assegnazione dell’attributo; il lavoro è completato quando lo snippet segnalato passa senza l’errore di valore restituito incompatibile e le varianti già funzionanti rimangono corrette.

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
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.