python / python/mypy

attribute failure when unpacking from tuple and wait_for

Offen
#15,569 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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)

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, die verlinkte mypy-play-Reproduktion auszuführen, und vergleiche die im Bericht gezeigten Varianten tuple, list, direct-call und variable-assignment. Verfolge, wie await wait_for mit tuple unpacking die Attributzuweisung behandelt; abgeschlossen ist die Aufgabe, wenn das gemeldete Snippet ohne den inkompatiblen Rückgabewertfehler durchläuft und die bereits funktionierenden Varianten weiterhin korrekt bleiben.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.