Fix false positive "Expected TypedDict key to be string literal" for Union[TypedDict, dict[K, V]]#21511
Merged
Merged
Conversation
…Union[TypedDict, dict[int, float]] When matching a dict literal against a union type containing both a TypedDict and a plain dict (e.g. `Union[Foo, dict[int, float]]`), `match_typeddict_call_with_dict` was emitting an error while probing whether the literal matched the TypedDict alternative. Suppress errors during matching so they are only reported during actual type checking. Fixes python#21510
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
ilevkivskyi
approved these changes
May 19, 2026
Member
ilevkivskyi
left a comment
There was a problem hiding this comment.
OK, this one looks reasonable.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a variable is typed as
Union[TypedDict, dict[int, float]], mypy incorrectlyraised
Expected TypedDict key to be string literalwhen assigning a dict literalwith non-string keys like
{1: 5.2}. The plaindict[int, float]alternative makesthe assignment valid, so this was a false positive.
The root cause was that
match_typeddict_call_with_dict, which is used as a probe tocheck whether a dict literal could match a TypedDict, was emitting errors from
validate_typeddict_kwargsduring the matching phase rather than silently returningFalse. Wrapping the call infilter_errors()suppresses those spurious diagnostics.Fixes #21510