Skip to content

stubtest: basic support for unpack kwargs#21024

Open
hauntsaninja wants to merge 1 commit intopython:masterfrom
hauntsaninja:stubtestunpack
Open

stubtest: basic support for unpack kwargs#21024
hauntsaninja wants to merge 1 commit intopython:masterfrom
hauntsaninja:stubtestunpack

Conversation

@hauntsaninja
Copy link
Collaborator

Fixes #21023

stub="def f2(**kwargs: Unpack[_Args]) -> None: ...",
runtime="def f2(*, a, c): pass",
error="f2",
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(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):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

stubtest doesn't expand **kwargs: Unpack[...]

2 participants