Expected TypedDict key to be string literal for Union of TypedDict and dict · python/typing · Discussion #2292 (original) (raw)
I'm getting error: Expected TypedDict key to be string literal [misc] from mypy for the following:
from typing import TypedDict
class Foo(TypedDict): some: str name: str
type Bar = dict[int, float]
type Baz = Foo | Bar
baz: Baz = {"some": "foo", "name": "x"} # OK bar: Bar = {1: 5.2, 2: 7.4} # OK
baz2: Baz = {1: 5.2} # error: Expected TypedDict key to be string literal [misc]
I would expect baz2 to be valid, or am I missing something?
It seems mypy does try to resolve to the dict[int, float]:
baz3: Baz = {"1": 5.2} # error: Dict entry 0 has incompatible type "str": "float"; expected "int": "float" [dict-item]
You must be logged in to vote
Yes, that seems like a bug in mypy. Zuban and ty have the same bug.
Yes, that seems like a bug in mypy. Zuban and ty have the same bug.
You must be logged in to vote
4 replies
Should I open an Issue for mypy or do you already know of one?
I don't recall an issue; I'd search for some terms like "union" "TypedDict" in the issue tracker first.
I also reported astral-sh/ty#3488 (apparently discussions don't get automatic backlinks?).