Skip to content

Is it possible to type hint tuple and dict equivalent to P.args and P.kwargs? #1447

Answered by erictraut
randolf-scholz asked this question in Q&A
Discussion options

You must be logged in to vote

No, there is no mechanism currently in the type system that enables this.

You can use unpack operators to make this work with the available mechanism, although some errors may go uncaught by current type checker implementations because this is not a typical usage pattern.

eval_func(mandatory_positional, *(1, 2), **{}) # No error (correct)
eval_func(mandatory_positional, *("1", 2), **{}) # Error in both mypy and pyright (correct)
eval_func(mandatory_positional, *(1, 2, 3), **{}) # Error in mypy, false negative in pyright
eval_func(mandatory_positional, *(), **{"a": 1, "b": 2})  # False negative in both mypy and pyright

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by randolf-scholz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants