Commit d23f5f0
Fix false positive "Expected TypedDict key to be string literal" for Union[TypedDict, dict[K, V]] (#21511)
When a variable is typed as `Union[TypedDict, dict[int, float]]`, mypy
incorrectly
raised `Expected TypedDict key to be string literal` when assigning a
dict literal
with non-string keys like `{1: 5.2}`. The plain `dict[int, float]`
alternative makes
the assignment valid, so this was a false positive.
The root cause was that `match_typeddict_call_with_dict`, which is used
as a probe to
check whether a dict literal could match a TypedDict, was emitting
errors from
`validate_typeddict_kwargs` during the matching phase rather than
silently returning
`False`. Wrapping the call in `filter_errors()` suppresses those
spurious diagnostics.
Fixes #21510
Co-authored-by: Claude Agent <agent@example.com>1 parent 5e0c274 commit d23f5f0
2 files changed
Lines changed: 14 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
932 | 932 | | |
933 | 933 | | |
934 | 934 | | |
935 | | - | |
| 935 | + | |
| 936 | + | |
936 | 937 | | |
937 | 938 | | |
938 | 939 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
941 | 941 | | |
942 | 942 | | |
943 | 943 | | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
944 | 956 | | |
945 | 957 | | |
946 | 958 | | |
| |||
0 commit comments