type annotated arguments in zip function lead to wrong return types when using the star operator
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 5.1k
- フォーク
- 2.1k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 82
説明
This issue is a followup to the following issue:
https://github.com/microsoft/pylance-release/issues/3598
Here's the code that is wrongly annotated:
test_list: list[tuple[int, str]] = [(1, 'testa'), (2, 'testb')]
a: tuple[int]
b: tuple[str]
a, b = zip(*test_list)
Pylance warns about an assignment of typle[int | str] to the tuples a and b.
The tuple annotation is being converted to an Iterator annotation, which does not support a sequence of types (see Eric's reply to the previous issue for details). Therefore, the resulting tuples after the unpacking operation are not as precisely typed as they could be.
And here is the typeshed code that leads to the 'wrong' type annotations:
I'm not sure if it is currently possible to fix this. I had the idea of adding a type hint to the *args keyword, so something like this:
@overload
def __new__(cls, __iter1: _T2:=Sequence[_T1], *args: _T2, strict: bool = ...) -> zip[tuple[_T1]]:
I'm unsure if this is valid python syntax and if it would result in the desired behavior. I'm also unsure how python would decide between the proposed constructor and the one that is currently being used during typechecking my example:
https://github.com/python/typeshed/blob/7b3fff714a48f0413141ba10344f9473e3ec4a18/stdlib/builtins.pyi#L1675-L1676
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、注釈付きの zip(*test_list) の例を再現し、stdlib/builtins.pyi の 1673-1715 行付近にある zip のオーバーロードを読みます。オーバーロードの変更を評価する前に、リンク先の typeshed issue と以前の Pylance issue を確認します。アンパックされた変数が代入に関する警告なしに tuple[int] と tuple[str] として推論されれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- devtools
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 38/100