I tried to instantiate an empty dictionary on the second level of an existing dict, then assign a key-value pair to it, but MyPy throws an error.
Here is a minimal example, which will reproduce it when MyPy checking is activated:
result = {"Test": "something"}
result['key'] = {}
result['key']['sub_key'] = ["some string", "another string"]
The error here will be something like:
mypy(error): Incompatible types in assignment (expression has type
"Dict[<nothing>, <nothing>]", target has type "List[str]")
How do I prevent this error? According to a similar problem, it was suggested to do
result['key'] = {} # type: ignore
as a workaround, but this does not seem very elegant, which is why I'm wondering if there is more one can do.