Skip to content

Safe conversion from TypedDict to Mapping not accepted #18439

Description

@dmoisset

Bug Report

Assignment of a TypedDict into a Mapping with a wider type is not allowed despite being safe.

To Reproduce

from typing import TypedDict, Mapping

class TD(TypedDict):
    a: str
    b: int

type Data = Mapping[str, str | int]

value = TD(a="hello", b=123)

x: Data = value

Playground link

Expected Behavior

The assignment should be accepted. Every value of TD is a valid instance of Data, and being a mapping Data does not allow mutation.

Actual Behavior

main.py:11: error: Incompatible types in assignment (expression has type "TD", variable has type "Mapping[str, str | int]")  [assignment]
Found 1 error in 1 file (checked 1 source file)

Your Environment

Running on python 3.13, latest mypy (1.14.1), default settings.

Activity

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

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions