-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Support Unpack
with dict
for keyword arguments: **kwargs: Unpack[dict[Literal["test"], ...]]
#17241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Also when implementing this, it might be useful to allow from collections.abc import Mapping
from typing import Literal, Unpack
def example(**kwargs: Unpack[Mapping[Literal["test"], int]]):
# ^^^^^^^
pass
example(test=123) |
Unpack
with dict
for function arguments: **kwargs: Unpack[dict[Literal["test"], ...]]
Unpack
with dict
for keyword arguments: **kwargs: Unpack[dict[Literal["test"], ...]]
I think you'd probably need to write a PEP if you want to expand the use of Unpack in this way |
What you're proposing here is not allowed by the typing spec currently. If you want to see something like this, you would need to extend the typing spec, probably through a new PEP as @AlexWaygood suggests. If you're interested in pursuing this, I recommend starting a discussion in the Python typing forum to see if you can gain support and consensus for such an idea. |
Posted to typing forum: https://discuss.python.org/t/proposal-support-unpack-dict-for-typing-keyword-arguments/53380 |
Bug Report
Mypy has support for declaring kwargs as
**kwargs: Unpack[SomeTypedDict]
.However, sometimes for dicts with uniform value types, it's more convenient to declare them in terms of
dict[Literal["key", "another"], ...]
. Other times it's a pragmatic work-around to the lack of support for@final
declaration forTypedDict
(#7981).To Reproduce
This should pass mypy checking:
Playground link
Actual Behavior
Rejected by mypy with the following error:
Your Environment
The text was updated successfully, but these errors were encountered: