python / python/typeshed

type annotation for `{dict_keys,dict_items}.is_disjoint` argument is too narrow

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

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

主要言語
Python
スター
5.1k
フォーク
2.1k
平均マージ
1日 19時間
マージ済み PR(30日)
82

説明

In the PR that added these annotations, @hauntsaninja said:

My instinct is to try it the stricter way and relax it if we ever get a real user complaint about it, but happy to go along with whatever others think is best

I'd like to provide such a complaint. In ty, we would like to infer precise key-type specializations (unions of string literals) of dict_keys and dict_items for closed TypedDicts, where all possible keys are known. We have real user requests for this, because it allows iterating over dict keys/items and passing the keys along to a function that expects a limited set of literals.

But doing this causes false positives with isdisjoint, because the following valid code will now error:

class TD(TypedDict, closed=True):
    x: int

def check(td: TD) -> None:
    # Both operations are safe at runtime but will report invalid-argument-type:
    td.keys().isdisjoint(["other"])
    td.items().isdisjoint([("other", 1)])

This is really a sub-issue of https://github.com/python/typeshed/issues/15271, the same category of issue as #6597.

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

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

はじめの一歩

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

調査の方向性

まず、PR 12309 で導入されたアノテーションと、issue #15271 の関連する議論を確認します。この issue にある 2 つの閉じた TypedDict の例を再現し、その後、正確なキー型とアイテム型を失うことなく両方の有効な呼び出しが受け入れられるように、is_disjoint 引数の型付けを調整します。結果として得られる型チェックの動作を検証します。

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

評価

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

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

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