TypedDict totality ignored when unpacked for function call
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
Bug Report
When unpacking a typeddict for a function call, mypy doesn't seem to check the totality of the typeddict, and instead assumes the keys are all present.
from typing import TypedDict
def my_function(foo: int, bar: int, baz: str) -> None:
pass
class MyTypedDict(TypedDict, total=False):
foo: int
bar: int
baz: str
a: MyTypedDict = {'bar': 1}
my_function(**a) # Should be error here, as a doesn't contain foo or baz
https://mypy-play.net/?mypy=latest&python=3.10&flags=strict&gist=5444e1100000a8f1678fe9cbcfc23921
Expected Behavior
Mypy should not assume that keys are present when the TypedDict has total=False, and treat it as potentially having any combination of they keys present.
Actual Behavior
Mypy allows this, but when executed it gives the following error:
Traceback (most recent call last):
File "<path>\typeddict1.py", line 17, in <module>
my_function(**a) # Should be error here, as a doesn't contain foo or baz
TypeError: my_function() missing 2 required positional arguments: 'foo' and 'baz'
#11753
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、issue またはその mypy-play リンクにある total=False の TypedDict アンパッキング例を再現し、次に TypedDict のアンパッキングに対する関数呼び出し引数のチェックを追跡します。オプショナルキーが欠落する可能性がある場合に mypy がエラーを報告し、必須キーについて有効な動作が維持されれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- devtools
- issue の種類
- バグ
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 48/100