Skip to content

Type-check all values for duplicate TypedDict keys - #21842

Open
Hmanbo wants to merge 1 commit into
python:masterfrom
Hmanbo:fix-typed-dict-duplicate-values
Open

Type-check all values for duplicate TypedDict keys#21842
Hmanbo wants to merge 1 commit into
python:masterfrom
Hmanbo:fix-typed-dict-duplicate-values

Conversation

@Hmanbo

@Hmanbo Hmanbo commented Aug 13, 2026

Copy link
Copy Markdown

Fixes #21841.

Summary

  • Preserve every explicitly written value for a duplicate TypedDict key so
    mypy checks each expression that Python evaluates.
  • Keep the final explicit value first for generic TypedDict inference while
    excluding synthetic TempNode values from overwritten dictionary unpack
    entries.
  • Add regression coverage for constructors, contextually typed dictionary
    literals, and duplicate keys followed by a dictionary unpack.

Tests

.venv\Scripts\python.exe -m pytest -n0 -k testTypedDictDuplicateKeysTypeCheckAllValues
.venv\Scripts\python.exe -m pytest -n0 mypy/test/testcheck.py::TypeCheckSuite::check-typeddict.test
.venv\Scripts\python.exe runtests.py self
.venv\Scripts\pre-commit.exe run --files mypy/checkexpr.py test-data/unit/check-typeddict.test
git diff --check

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Duplicate keys cause earlier TypedDict values to be skipped during type checking

1 participant