You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Duplicate keys cause earlier TypedDict values to be skipped during type checking #21841
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.
$ mypy --show-error-codes --no-incremental example.pySuccess: 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:
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:
My plan is to preserve all explicit values associated with a duplicate key
for type checking, while keeping the final value first for TypedDict generic
type inference. I'll also add regression tests for both TypedDict constructor
calls and contextually typed dictionary literals.
Activity
Hmanbo commentedon Aug 13, 2026
I'd like to work on this.
My plan is to preserve all explicit values associated with a duplicate key
for type checking, while keeping the final value first for TypedDict generic
type inference. I'll also add regression tests for both TypedDict constructor
calls and contextually typed dictionary literals.
Does this approach sound reasonable?