allow consistent treatment of kwargs: Unpack[TD]#2272
Open
carljm wants to merge 1 commit intopython:mainfrom
Open
allow consistent treatment of kwargs: Unpack[TD]#2272carljm wants to merge 1 commit intopython:mainfrom
carljm wants to merge 1 commit intopython:mainfrom
Conversation
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.
I think that the conformance suite (and the behavior in current type checkers) of
**kwargs: Unpack[TD]is inconsistent with itself, and with the language in the spec that was added to accommodate PEP 728 (closedandextra_items).The conformance suite asserts this (simplified for clarity):
But the spec (since PEP 728) says that if
TDallows extra items, then those should be accepted in calls. And it also says that aTypedDictwithout an explicitclosedorextra_itemsparameter usually behaves as if it hasextra_itemsof typeReadOnly[object].Thus I think the extra argument in the call above should be allowed, even though current type checkers disallow it.
Some type checkers (pyright and pyrefly) allow this call:
It seems clear that this second call should be allowed, because TD2 is a subtype of TD1 and assignable to it, so
**td2should be acceptable for**kwargs: Unpack[TD1]. But if this call (which definitely provides a keyword argumentv2) is allowed, then it's inconsistent not to also allow the above call that explicitly providesv2.If
fshould not accept arbitrary additional keyword arguments, it should instead use**kwargs: Unpack[ClosedTD], whereClosedTDisclosed=True.In addition, the spec and conformance suite now require assignability of callables using
**kwargs: Unpack[TD]to behave in a way that accounts for this possibility of additional keyword arguments being passed. It's inconsistent to require callable assignability to account for this, yet not allow calls to actually make use of it. In ty, we want to implementUnpack[TD]in such a way that it can be treated as simply syntax sugar, by transforming**kwargs: Unpack[TD]into a set of explicit typed keyword arguments and (possibly, for a non-closed TypedDict) a preserved**kwargsparameter. I think this approach is clearly desirable for consistent and predictable behavior, as well as for simplicity of implementation. But the current conformance suite is inconsistent with itself in a way that makes such an implementation impossible.Personally I believe the
# Eon this line should simply be removed, and conformant implementation should be required to allow this call. But I'm proposing here to simply relax the assertion to# E?in order to respect backward compatibility and not put existing type-checkers into non-conformance.