-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Fix type inference for TypedDict unpacking in dictionary displays #11611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # This sample tests dictionary expansion for TypedDicts without an expected | ||
| # TypedDict target type context. | ||
|
|
||
| from typing import NotRequired, TypedDict, reveal_type | ||
|
|
||
|
|
||
| class HomogeneousTD(TypedDict): | ||
| a: int | ||
| b: int | ||
|
|
||
|
|
||
| class HeterogeneousTD(TypedDict): | ||
| a: int | ||
| b: str | ||
|
|
||
|
|
||
| class OptionalTD(TypedDict): | ||
| a: int | ||
| b: NotRequired[float] | ||
|
|
||
|
|
||
| def test_homogeneous(td: HomogeneousTD): | ||
| res1 = {**td} | ||
| reveal_type(res1, expected_text="dict[str, int]") | ||
|
|
||
|
|
||
| def test_heterogeneous(td: HeterogeneousTD): | ||
| res2 = {**td} | ||
| reveal_type(res2, expected_text="dict[str, int | str]") | ||
|
|
||
|
|
||
| def test_optional(td: OptionalTD): | ||
| res3 = {**td} | ||
| reveal_type(res3, expected_text="dict[str, int | float]") | ||
|
|
||
|
|
||
| def test_multiple(td1: HomogeneousTD, td2: HeterogeneousTD, td3: OptionalTD): | ||
| res4 = {**td1, **td2, **td3} | ||
| reveal_type(res4, expected_text="dict[str, int | str | float]") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The added cases do not cover the changed |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing this guard makes the block run when an expected TypedDict target is present. Closed TypedDicts populate
extraItemswithNever, so unpacking one into an expected TypedDict now contributes a non-literalstrkey thatassignToTypedDictrejects, producing a new assignment error. Retain the context gate while removing only the fallback:if (!expectedTypedDictEntries && tdEntries.extraItems) { ... }.