Duplicate keys cause earlier TypedDict values to be skipped during type checking
まだ誰も着手していません。
評価
調査の方向性
mypy/checkexpr.py の839~842行目付近から始め、提供された mypy コマンドを使って example.py で問題を再現します。重複した TypedDict キーが以前の式を置き換える仕組みを追跡します。完了条件は、すべての値式が型チェックされ、parse(123) のケースで期待される arg-type エラーが報告されることです。
索引モデルが issue の本文から書いたものです。
説明
Bug Report
When a dictionary literal used to construct a TypedDict contains duplicate
keys, mypy only type-checks the final value for that key. Earlier value
expressions are skipped entirely.
This can hide real errors because Python still evaluates every value expression
in the dictionary literal, including values that are later overwritten by a
duplicate key.
To Reproduce
from typing import TypedDict
class Payload(TypedDict):
value: int
def parse(value: str) -> int:
return len(value)
payload = Payload({"value": parse(123), "value": 0})
$ mypy --show-error-codes --no-incremental example.py
Success: no issues found in 1 source file
Running the program demonstrates that the skipped expression is evaluated:
TypeError: object of type 'int' has no len()
Expected Behavior
Mypy should type-check every value expression in the dictionary literal, even
if a later duplicate key determines the final value of the TypedDict field.
In this example, it should report an error similar to:
error: Argument 1 to "parse" has incompatible type "int"; expected "str" [arg-type]
The same expression is correctly rejected when the target is a regular
dict[str, int]. The issue also occurs with a contextually typed literal:
payload: Payload = {"value": parse(123), "value": 0}
Actual Behavior
Mypy reports no issues because only the last value associated with the
duplicate key is checked.
Additional Context
The current implementation replaces the previously collected expression
without type-checking it and has a TODO to type-check all duplicate-key values:
PR #15425 also mentioned repeated TypedDict keys as a known limitation, but I
could not find an existing issue that tracks it:
https://github.com/python/mypy/pull/15425
Environment Used to Reproduce
- Mypy versions used:
mypy 2.3.0 (compiled: yes)mypy 2.4.0+dev.6eb141f03a8b43c274f15cc4978d617a71d8f37e (compiled: no)
- Mypy command-line flags:
--show-error-codes --no-incremental - Mypy configuration options: none
- Python version used: 3.12.8
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- 平均マージ
- 1日 18時間
- マージ済み PR(30日)
- 54
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
python/mypy のほかの issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
bug topic-configuration topic-error-reporting
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
似ている issue
-
link-check link-check:sphinx-theme
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
qgis/QGIS-Documentation#11275 ·
-
bug priority:normal ready-for-dev
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
OpenHands/extensions#626 · コメント 1 件 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
CSCfi/sd-search-api#39 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 90/100