Skip to content

dict with literal keys cannot be unpacked into a dict with str keys #19893

@injust

Description

@injust

Bug Report

A dict that is typed with literal keys cannot be unpacked into a dict with string keys.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.13&gist=f150b9f1c64eba97cb0496a6a5d7a2b6

from typing import Literal

data: dict[Literal["foo"], str] = {"foo": "bar"}


def foo() -> dict[str, str]:
    return {**data}

Expected Behavior

The code should type check. It works fine in Pyright.

Actual Behavior

error: Unpacked dict entry 0 has incompatible type "dict[Literal['foo'], str]"; expected "SupportsKeysAndGetItem[str, str]"  [dict-item]

Your Environment

  • Mypy version used: 1.18.2
  • Python version used: 3.13

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions