Skip to content

Support Unpack with dict for keyword arguments: **kwargs: Unpack[dict[Literal["test"], ...]] #17241

Closed as not planned

Description

@intgr

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

Activity

  1. added
    bug
    on May 14, 2024
  2. intgr commented on May 14, 2024

    @intgr
    ContributorAuthor

    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)
  3. 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"], ...]]`[/+] on May 14, 2024
  4. AlexWaygood commented on May 14, 2024

    @AlexWaygood
    Member

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

  5. erictraut commented on May 14, 2024

    @erictraut

    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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions