python / python/mypy

attribute failure when unpacking from tuple and wait_for

オープン
#15,569 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

bug
主要言語
Python
スター
20.6k
フォーク
3.3k
平均マージ
1日 18時間
マージ済み PR(30日)
54

説明

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)

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、リンクされている mypy-play の再現コードを実行し、レポートに示されている tuple、list、direct-call、variable-assignment の各バリエーションを比較します。tuple unpacking を伴う await wait_for が属性の代入をどのように処理するかを追跡します。報告されたスニペットが互換性のない戻り値エラーなしで通過し、既存の動作するバリエーションが引き続き正しく動作すれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
devtools
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。