Keys and values could be narrower for closed TypedDicts
オープン
まだ誰も着手していません。
feature
topic-typed-dict
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
I was very excited to try out mypy 2.2 with the initial support for the TypedDict closed keyword.
But in our code base, where we often iterate over TypedDicts, it did not help much because the keys and values are still inferred as str and object, respectively.
from typing_extensions import TypedDict, reveal_type
class Closed(TypedDict, closed=True, total=False):
a: int
b: str
c = Closed(a=1, b='a')
reveal_type(c.items())
for key, val in c.items():
c[key]
$ mypy --pretty test.py
test.py:9: note: Revealed type is "_collections_abc.dict_items[str, object]"
test.py:12: error: TypedDict key must be a string literal; expected one of ("a", "b") [literal-required]
c[key]
^~~
Found 1 error in 1 file (checked 1 source file)
ty narrows both keys and values here
$ ty check test.py
ty check test.py
info[revealed-type]: Revealed type
--> test.py:9:13
|
9 | reveal_type(c.items())
| ^^^^^^^^^^ `dict_items[Literal["a", "b"], int | str]`
|
Found 1 diagnostic
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、mypy --pretty を使って issue の test.py の例を再現し、TypedDict.items() とそれに続く c[key] アクセスに焦点を当てます。これらの推論された型を担う TypedDict の型チェック経路を追跡します。完了条件は、閉じた TypedDict の反復でキーのリテラルユニオンと対応する値の型が明らかになり、反復されたキーによるインデックスアクセスが受け入れられることです。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- devtools
- issue の種類
- 機能追加
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 静か
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 45/100