Repository navigation
Update unpack TypedDict kwargs forwarding spec + add conformance tests - #2338
yangdanny97 wants to merge 31 commits into
Conversation
|
This issue is nuanced, because many unpacking behaviors in Python are unsafe. If you unpack a 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 We decided to be stricter with 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. |
|
For Pyrefly my current thought is that this would be off-by-default and enabled in strict mode. |
|
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? |
|
So do you think that we should just merge #1960 and delete this section from the typing spec entirely? Or weaken the first assertion to "may" and "E?" |
|
I would probably land this with the first assertion weakened (and discussion of that in the spec also), but that's not a strong preference, it just matches what we decided to do in ty, so naturally it already makes sense to me :) Very open to other opinions here. |
|
Does it make sense to differentiate between implicitly open and explicitly open ( |
It might. Currently in ty we only apply the extra strictness with |
|
The current TypedDict spec says that In the abstract I'd prefer to treat open TypedDicts similar to ones with |
carljm
left a comment
There was a problem hiding this comment.
I think there are a couple details here in the spec (one backwards statement, one broken link) that should be addressed -- everything else here is up to you whether you want to include it in this PR. Otherwise this looks ready to me.
I'm glad we are clearing this up now, before wider adoption of closed/extra_items TypedDicts.
carljm
left a comment
There was a problem hiding this comment.
I'm happy with this.
I guess since it touches the spec and the conformance suite, we need a Discuss thread, the one-week wait, and an issue for Typing Council sign-off?
|
Sorry for taking such a long time here. I have tried to play with this quite a bit in Zuban to get a feel for it and I'm favorable to merging this PR. There have been two potential cases that I would like to add to the spec (please let me know if you disagree): This is currently not an error in Zuban/Mypy (test is called Another case of callable subtyping that I looked at was this one that might be worth to add: Before my changes to think of non-closed TypedDicts in Unpacks as essentially having (I'm aware that this might not be an issue in other type checker, because it just widens the type. But you can also create this case with Protocols like I'm also open to the make some of the |
20a9e41 to
770b585
Compare
|
Okay, based on the dsicussion in the forum thread, I've updated the docs and tests to require the error. |
|
I haven't had time to look into @davidhalter's examples yet, will do so when i hvae time |
carljm
left a comment
There was a problem hiding this comment.
Looks good! A few things I noticed inline.
Summary: python/typing#2338 added a bunch of conformance tests and rules in the spec. This PR adds those test cases to Pyrefly's suite and makes Pyrefly pass them. Only the open typed dict rules get the special open-unpacking error code to be off-by-default, the rest get normal errors. Pull Request resolved: #4714 Test Plan: added tests Reviewed By: kinto0 Differential Revision: D117964929 Pulled By: rchen152 fbshipit-source-id: 031d4578f0a2688f7675413f13f20a6dd70c35c0
|
bump |
|
Oops, sorry we let this one drop. @davidhalter and @jorenham it looks like we need your approvals on python/typing-council#64 in order to move forward here. @yangdanny97 once we've got those approvals in principle, do you mind rebasing the PR? Then I'd give it a final once-over and merge it. |
|
I intentionally did not check the box, because @yangdanny97 wrote that he wants to look at my examples:
I don't think this has happened yet, I don't find any comments or commits about it. |
|
Thanks @yangdanny97 |
Summary: Adds 2 more test cases for David Halter's comments on python/typing#2338 (comment) In the following excerpt below (see linked comment for context) ``` if bool(): a1 = a2 # (1) This should error else: a2 = a1 # (2) ``` Pyrefly currently does not error on 1; this PR also fixes that behavior The PR only adds one rule: the target's extra-kwarg type must also be assignable to every source keyword parameter the target doesn't name. The actual delta is larger because Claude found a simpler representation of the rules that uses fewer match cases - [x] I am a human, and I fully understand the code I am submitting. Pull Request resolved: #5756 Test Plan: test.py wait for primer Reviewed By: stroxler Differential Revision: D134402139 fbshipit-source-id: ef9f4874b84efb389263ff57c72367555f0b7f93
|
addressed the smaller comments for now, will look at the bigger suggested rewrites tomorrow |
carljm
left a comment
There was a problem hiding this comment.
Thank you for your patience with multiple iterations here! A few more comments, but this looks much better to me.
jorenham
left a comment
There was a problem hiding this comment.
Looks good! I just have some minor nits.
|
Here's an example where mypy and pyright fail on signatures that should be equivalent using the desugaring rules. The other 4 checkers tracked on the leaderboard accept this example. They are equivalent, but I wonder if we should allow type checkers to optionally error here, like how checkers can already error when explicitly passing unknown keyword arguments to a function with open TypedDict kwargs. That would align with the "open TypedDicts may contain unknown values but we should discourage it" vibe if that's what we're going for. |
|
Made some major edits and reorderings to merge/delete some redundancies, and added a bunch of test cases for the desugaring rules and other examples that came up |
carljm
left a comment
There was a problem hiding this comment.
This is looking like a huge improvement to me, and a significant reduction in the size of the spec text. Thank you! A few comments.
| runtime. Even though ``foo`` expects ``kwargs`` of type ``Animal`` it doesn't | ||
| matter if it receives additional arguments because it only reads and uses what | ||
| it needs completely ignoring any additional values. | ||
| Passing a dictionary of type ``dict[str, object]`` as a ``**kwargs`` argument |
There was a problem hiding this comment.
I know this wording predates the PR, but I think the blanket rule here conflicts with the new desugaring rules. For example,
class Bag(TypedDict, extra_items=object):
pass
def f(**kwargs: Unpack[Bag]) -> None: ...
def forward(d: dict[str, object]) -> None:
f(**d) # OKf desugars to def f(**kwargs: object) -> None, so this is safe. All the checkers I tried that support extra_items accept it.
Could we drop the blanket prohibition, explain why dict[str, object] doesn't work for the particular Movie example below, and (maybe? I don't feel strongly) add a positive test like this one?
There was a problem hiding this comment.
The following example without extra_items would pass as well since f desugars to def f(x: object, y: object) -> None, which can normally be called with an unpackeddict[str, object].
from typing import TypedDict, Unpack
class Bag(TypedDict):
x: object
y: object
def f(**kwargs: Unpack[Bag]) -> None: ...
def forward(d: dict[str, object]) -> None:
f(**d) # OK
If the desired behavior comes naturally as a consequence of desugaring and no extra rules need to be applied, we should just delete the whole section, since it basically contains 1) the wrong rule and 2) "behavior might vary across type checkers"
WDYT?
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:
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.