Skip to content

Update unpack TypedDict kwargs forwarding spec + add conformance tests - #2338

Open
yangdanny97 wants to merge 1 commit into
python:mainfrom
yangdanny97:typeddict-kwargs-passing
Open

Update unpack TypedDict kwargs forwarding spec + add conformance tests#2338
yangdanny97 wants to merge 1 commit into
python:mainfrom
yangdanny97:typeddict-kwargs-passing

Conversation

@yangdanny97

Copy link
Copy Markdown
Contributor

Previously, this section of the spec was not exercised at all in the conformance tests, and also was not implemented by any type checker.

This PR updates the spec to account for closed & extra_items TypedDicts, and adds conformance tests.

There are 3 asserted errors:

  • open TypedDict kwargs being unpacked into function w/o kwargs (no type checker implements this)
  • extra_items TypedDict kwargs being unpacked into function w/o kwargs (only ty implements this)
  • extra_items TypedDict kwargs being unpacked into function w/ incompatibly-typed kwargs (ty, pyright, and zuban implement this)

This would supersede #1960, which proposed that we delete the section of the spec entirely.
@rchen152's comment in https://discuss.python.org/t/typing-spec-inconsistency-for-unpacking-typed-dict-kwargs/79640 favors deletion.

@yangdanny97 yangdanny97 changed the title Add tests for unpack TypedDict kwargs forwarding Update unpack TypedDict kwargs forwarding spec + add conformance tests Aug 11, 2026
@carljm

carljm commented Aug 11, 2026

Copy link
Copy Markdown
Member

This issue is nuanced, because many unpacking behaviors in Python are unsafe. If you unpack a list[int] into a function that takes (int, int) (but not *args: int), every type checker allows that, even though the list could obviously contain fewer or more than two elements. The collective decision seems to be that since length is not part of what the list type encodes, we trust the user rather than erroring.

In ty we made the decision not to error on the first case here, because we saw large mypy-primer fallout if we enforced this rule, and we felt it was analogous to the case of list[int] -- an open TypedDict may contain extra elements (just like a list may contain any number of elements), but the type doesn't explicitly say that it does, so we can be forgiving here.

We decided to be stricter with extra_items, in part because there is no widespread usage yet, so usage will adapt to what type checkers permit, and in part because in this case the user has explicitly declared their intent for the TypedDict to contain extra items, so it feels reasonable to account for that more strictly. This does mean that implicitly open typed dicts are not equivalent to extra_items=ReadOnly[object], which maybe in principle they would be -- but I think this is currently true in all type checkers.

I think ty's compromise is reasonable, but I'm not totally convinced it's the best option, open to other resolutions here. But enforcing this rule on all implicitly open TypedDicts will have a significant impact on existing real-world code.

@yangdanny97

Copy link
Copy Markdown
Contributor Author

For Pyrefly my current thought is that this would be off-by-default and enabled in strict mode.

@carljm

carljm commented Aug 11, 2026

Copy link
Copy Markdown
Member

That seems reasonable. I'm not sure if behavior that we wouldn't turn on by default in our own type checkers should be encoded as a conformance suite requirement, though?

It seems to me that a strict mode which requires safety in unpacking an open TypedDict should probably also require safety in unpacking a list/Sequence?

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.

2 participants