Skip to content

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

Closed
intgr opened this issue May 14, 2024 · 5 comments
Labels
bug mypy got something wrong

Comments

@intgr
Copy link
Contributor

intgr commented May 14, 2024

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 for TypedDict (#7981).

To Reproduce

This should pass mypy checking:

from typing import Literal, Unpack

def example(**kwargs: Unpack[dict[Literal["test"], int]]):
    #                        ^^^^
    pass

example(test=123)

Playground link

Actual Behavior

Rejected by mypy with the following error:

error: Unpack item in ** argument must be a TypedDict [misc]

Your Environment

  • Mypy version used: 1.10.0
  • Python version used: 3.12.3
@intgr intgr added the bug mypy got something wrong label May 14, 2024
@intgr
Copy link
Contributor Author

intgr commented May 14, 2024

Also when implementing this, it might be useful to allow Mapping/MutableMapping in these contexts as well:

from collections.abc import Mapping
from typing import Literal, Unpack

def example(**kwargs: Unpack[Mapping[Literal["test"], int]]):
    #                        ^^^^^^^
    pass

example(test=123)

@intgr intgr changed the title Support Unpack with dict for function arguments: **kwargs: Unpack[dict[Literal["test"], ...]] Support Unpack with dict for keyword arguments: **kwargs: Unpack[dict[Literal["test"], ...]] May 14, 2024
@AlexWaygood
Copy link
Member

I think you'd probably need to write a PEP if you want to expand the use of Unpack in this way

@erictraut
Copy link

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.

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale May 14, 2024
@intgr
Copy link
Contributor Author

intgr commented May 15, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

3 participants