ty icon indicating copy to clipboard operation
ty copied to clipboard

Support inferring a call to `dict(...)` in TypedDict context

Open carljm opened this issue 4 weeks ago • 1 comments

Originally reported at https://www.reddit.com/r/Python/comments/1podix2/comment/nuhhob3/

from typing import TypedDict

class TD(TypedDict):
    x: int

t: TD = dict(x=1) # error: "Object of type `dict[str, int]` is not assignable to `TD`"

https://play.ty.dev/63687f42-6944-4a20-b04f-94a38b171e73

carljm avatar Dec 20 '25 01:12 carljm

I'm going to try this over the weekend if that's ok :)

Hugo-Polloli avatar Dec 20 '25 11:12 Hugo-Polloli

Despite the recommenation of not using mutable data structures as default input arguments, the following code snippet would not be working despite the fix. Is this expected?

def func(foo: TD = dict(x=1)) -> None:
    ...

(or even with the {"x": 1})

mflova avatar Dec 20 '25 22:12 mflova