Skip to content

attribute failure when unpacking from tuple and wait_for #15569

@bentheiii

Description

@bentheiii

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions