Unable to pass values from TypedDict into dict #9117
Labels
bug
mypy got something wrong
false-positive
mypy gave an error on correct code
topic-typed-dict
topic-usability
Not sure if this is strictly a bug, however it's not a behaviour I expected and I can't see anything in
mypy
's docs either way.If you have a
TypedDict
such as:and you want to combine that mapping with another using either spray or
.update()
forms then this is considered an error:note however that passing only into a
dict
works:Given the type of
x
here I can see why thed.update(..)
form doesn't work, though I would have expected thatmypy
would be able to merge the types when using spray notation.Is this expected behaviour? If so, what is the expected way to combine a
TypedDict
with additional keys like this?Aside: I would also expect that the signature of
x
beDict[str, int]
rather thanDict[str, object]
since the type details of theTypedDict
are essentially that. (I would expect aUnion
in the value type of more complicatedTypedDict
s).My use-case is preparing a dict for conversion to JSON for a web API, so I'm currently working around this by annotating
d
as beingDict[str, Any]
which I end up doing anyway for JSON and which sidesteps the inference of the dict members to find the type ford
.I'm using mypy 0.782 on Python 3.6.
The text was updated successfully, but these errors were encountered: