type inferance from await asyncio.gather() with 6 elements no longer works
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- 平均マージ
- 1日 18時間
- マージ済み PR(30日)
- 54
説明
Bug Report
mypy doesn't seem to properly infer types when unpacking the results of an asyncio.gather() that has at least 6 elements.
This leads to the unpacked variables getting assigned the object type, and then future uses of the variable cause typing issues.
To Reproduce
import asyncio
import datetime
async def get_str_value() -> str:
await asyncio.sleep(1)
return 'value'
async def get_datetime_value() -> datetime.datetime:
await asyncio.sleep(2)
return datetime.datetime.now()
async def run() -> None:
(str1_value,
str2_value,
str3_value,
datetime1_value,
str4_value,
str5_value,
datetime2_value) = reveal_type(await asyncio.gather(get_str_value(),
get_str_value(),
get_str_value(),
get_datetime_value(),
get_str_value(),
get_str_value(),
get_datetime_value()))
reveal_type(str1_value)
reveal_type(datetime1_value)
print(f"str={str1_value}, date={datetime1_value}")
asyncio.run(run())
Expected Behavior
mypy should be able to infer the appropriate type of the values from the gathered list. From searching, it looks like this might be a typeshed issue, or it might be something in mypy itself. It is unclear what can be done here.
Actual Behavior
mypy infers the return of the await as a list, and then assigns the object type to each of the variables. This results in cascading type errors as other code assumes those variables would be the appropriate type.
If you only have 5 values, then this correctly unpacks the tuple into the specific variables. But as soon as you gather 6 items it breaks.
asyncio-gather-test.py:21: note: Revealed type is "tuple[builtins.str, builtins.str, builtins.str, datetime.datetime, builtins.str, builtins.str]"
asyncio-gather-test.py:29: note: Revealed type is "builtins.str"
asyncio-gather-test.py:30: note: Revealed type is "datetime.datetime"
Your Environment
- Mypy version used: 1.9, I also tried a development build
- Mypy command-line flags: mypy
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.12.2
I'm really not sure what a good workaround in my own code would be here. For now I've taken to using asserts to check the types, which works ok, but is verbose.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、提供された asyncio.gather の例を再現し、5 個の引数と 6 個以上の引数を比較して、reveal_type の出力を確認します。修正が typeshed と mypy のどちらに属するのかを判断し、報告された 7 要素の呼び出しで異種の結果がそれぞれの型を保持し、連鎖的なエラーが発生しなければ完了とします。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- devtools
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 45/100