python / python/mypy

attribute failure when unpacking from tuple and wait_for

Open
#15,569 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the linked mypy-play reproduction and compare the tuple, list, direct-call, and variable-assignment variants shown in the report. Trace how await wait_for with tuple unpacking handles the attribute assignment; done means the reported snippet passes without the incompatible return-value error while the existing working variants remain correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.