Variadic tuple unpacking requires a star target -- why?
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
Bug Report
When attempting to unpack a tuple that uses Unpack, if you don't use a star target to capture the variadic tuple as a list, you get the error Variadic tuple unpacking requires a star target.
This is overly strict and prescriptivist and is not even applied consistently between similar type concepts within mypy.
- You definitely can do this if the lengths check out.
pyrightdoesn't raise an error like this. You are simply on your own for length checking the unpacking of indeterminate lengthtuples, just like you are forlist, another indeterminate length unpackable sequence.- In fact, this is also how
mypytreats unpackinglist. It doesn't raise an error if you don't use a star target. - I have a real use case where I want to do exactly this. I lay out my use case in this bug report to
pyright: https://github.com/microsoft/pyright/issues/7987
To Reproduce
from typing import Tuple
from typing_extensions import Unpack
def foo() -> Tuple[int, Unpack[Tuple[str, ...]]]:
return 1, "bar", "baz"
a, b, c = foo() # error: Variadic tuple unpacking requires a star target [misc]
d, e, f = [1, "bar", "baz"] # OK
Gist URL: https://gist.github.com/mypy-play/4014976ca069e54b5c1fe86a45353f58
Playground URL: https://mypy-play.net/?mypy=master&python=3.12&gist=4014976ca069e54b5c1fe86a45353f58
Expected Behavior
No errors in the above example. a's type should be revealed to be int, and b and c's types should be revealed to be str.
Actual Behavior
An error, and a, b, and c are all revealed to be Any.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
レポートまたはその mypy-play URL にある最小再現から始め、タプルとリストのアンパックについて診断と表示される型を比較します。完了条件は、タプルの代入でエラーが発生せず、a が int、b と c が str として表示され、リストの場合にリグレッションが発生しないことです。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 42/100