stubtest: basic support for unpack kwargs#21024
Open
hauntsaninja wants to merge 1 commit intopython:masterfrom
Open
stubtest: basic support for unpack kwargs#21024hauntsaninja wants to merge 1 commit intopython:masterfrom
hauntsaninja wants to merge 1 commit intopython:masterfrom
Conversation
ec469ec to
25f186b
Compare
| stub="def f2(**kwargs: Unpack[_Args]) -> None: ...", | ||
| runtime="def f2(*, a, c): pass", | ||
| error="f2", | ||
| ) |
Collaborator
There was a problem hiding this comment.
I'd suggest adding some cases for mismatched optional parameters (e.g. optional in the TypedDict but required at runtime, and vice versa)
Comment on lines
+949
to
+958
| stub_sig.kwonly[key_name] = nodes.Argument( | ||
| nodes.Var(key_name, key_type), | ||
| type_annotation=key_type, | ||
| initializer=( | ||
| nodes.EllipsisExpr() | ||
| if key_name not in typed_dict_arg.required_keys | ||
| else None | ||
| ), | ||
| kind=nodes.ARG_NAMED, | ||
| pos_only=False, |
Collaborator
There was a problem hiding this comment.
Testing this locally, I think you need to set kind to ARG_NAMED_OPT too for optional parameters to be handled correctly.
Suggested change
| stub_sig.kwonly[key_name] = nodes.Argument( | |
| nodes.Var(key_name, key_type), | |
| type_annotation=key_type, | |
| initializer=( | |
| nodes.EllipsisExpr() | |
| if key_name not in typed_dict_arg.required_keys | |
| else None | |
| ), | |
| kind=nodes.ARG_NAMED, | |
| pos_only=False, | |
| optional = key_name not in typed_dict_arg.required_keys | |
| stub_sig.kwonly[key_name] = nodes.Argument( | |
| nodes.Var(key_name, key_type), | |
| type_annotation=key_type, | |
| initializer=nodes.EllipsisExpr() if optional else None, | |
| kind=nodes.ARG_NAMED_OPT if optional else nodes.ARG_NAMED, | |
| pos_only=False, |
Collaborator
There was a problem hiding this comment.
(I'm also not sure if setting initializer=nodes.EllipsisExpr() is necessary. I wasn't able to find a difference between using EllipsisExpr vs always using None)
Comment on lines
+773
to
+775
| from typing import TypedDict, Unpack | ||
|
|
||
| class _Args(TypedDict): |
Collaborator
There was a problem hiding this comment.
So that I won't have to update #19574 again :-)
Suggested change
| from typing import TypedDict, Unpack | |
| class _Args(TypedDict): | |
| from typing import TypedDict, Unpack, type_check_only | |
| @type_check_only | |
| class _Args(TypedDict): |
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.
Fixes #21023