Incorrect assignability with union involving TypedDict (original) (raw)

Summary

On this program:

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]

ty outputs

error[missing-typed-dict-key]: Missing required key 'name' in TypedDict `Foo` constructor
  --> main.py:14:13
   |
14 | baz2: Baz = {1: 5.2} # error: Expected TypedDict key to be string literal  [misc]
   |             ^^^^^^^^
   |

error[missing-typed-dict-key]: Missing required key 'some' in TypedDict `Foo` constructor
  --> main.py:14:13
   |
14 | baz2: Baz = {1: 5.2} # error: Expected TypedDict key to be string literal  [misc]
   |             ^^^^^^^^
   |

Found 2 diagnostics

But the program should be accepted; the Baz union includes dict[int, float] and this is a valid dict[int, float].

(From python/typing#2292)

Version

No response