Reputation: 45291
I have something like this (very simplified):
# mymodule.py
from typing import TypedDict, cast
D=TypedDict('D', {'x':int, 'y':int})
d = {}
d['x']=1
d['y']=2
d = cast(D, d)
But mypy complains:
mymodule.py:9: error: Incompatible types in assignment (expression has type "D", variable has type "Dict[str, int]") Found 1 error in 1 file (checked 1 source file)
Shouldn't it be valid to cast a vanilla dictionary to a TypedDict
subtype? If not, what is the correct way to "build-up" a dictionary and then declare its type afterwards?
Note that this is very simplified; in actuality the dictionary is being built from a more complicated algorithm than given above.
UPDATE: the problem seems to persist even when I change the variable name rather than try to cast the type.
# mymodule.py
from typing import TypedDict, cast
D=TypedDict('D', {'x':int, 'y':int})
d = {}
d['x']=1
d['y']=2
dd: D = d
error: Incompatible types in assignment (expression has type "Dict[str, int]", variable has type "D") Found 1 error in 1 file (checked 1 source file)
Upvotes: 4
Views: 6807
Reputation: 52089
Type-cast the initial dict before assigning it:
from typing import TypeDict, cast
D = TypedDict('D', {'x':int, 'y':int})
d = cast(D, {})
d['x']=1
d['y']=2
This ensures that the variable d
is directly inferred as "a D
". Otherwise, inference immediately locks in the variable d
as a Dict[..., ...]
which cannot be changed later on.
Upvotes: 5
Reputation: 45291
A solution seems to be to use a new variable name when the type cast is executed:
# mymodule.py
from typing import TypedDict, cast
D=TypedDict('D', {'x':int, 'y':int})
d = {}
d['x']=1
d['y']=2
dd: D = cast(D, d)
I suppose this enforces a best-practice of using a new variable name when signifying to the reader that the keys are not going to change from this point forward. It seems a little bit excessive to me, but I can understand why it would be advantageous in general for the type checker to treat things this way. EDIT: the accepted answer is a better way!
Upvotes: 1