python / python/mypy

TypedDict totality ignored when unpacked for function call

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

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

bug topic-typed-dict
主要言語
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

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

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

はじめの一歩

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

調査の方向性

まず、issue またはその mypy-play リンクにある total=False の TypedDict アンパッキング例を再現し、次に TypedDict のアンパッキングに対する関数呼び出し引数のチェックを追跡します。オプショナルキーが欠落する可能性がある場合に mypy がエラーを報告し、必須キーについて有効な動作が維持されれば完了です。

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

評価

技術スタック
python
領域
devtools
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
48/100

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

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