Search code examples
pythonpython-typingmypy

type casting a built-up dictionary to a TypedDict


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)


Solution

  • 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.