KEMBAR78
Unexpected error when assigning dict literal to union containing TypedDict · Issue #19590 · python/mypy · GitHub
Skip to content

Unexpected error when assigning dict literal to union containing TypedDict #19590

@samwgoldman

Description

@samwgoldman

In general, I'd assume x: T | U = e to succeed without error if e is assignable to either T or U. In the below code, the dict literal is not assignable to the typed dict TD, but it is assignable to dict[str, str]. Although the typed dict case does fail, I don't expect to see any type errors.

from typing import TypedDict

class TD(TypedDict):
    x: int

x: TD | dict[str, str] = { "x": "foo" }

Expected behavior: No error

Actual Behavior: Incompatible types (expression has type "str", TypedDict item "x" has type "int") [typeddict-item]

Mypy version used: 1.17.0 (playground)
Mypy command-line flags: playground defaults
Mypy configuration options from mypy.ini (and other config files): N/A
Python version used: 3.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions