I am using Python 3.8.1 and mypy 0.782. I don't understand why mypy complains about the following code:
from typing import Union, List, Dict
Mytype = Union[Dict[str, str], Dict[str, List[str]]]
s: Mytype = {"x": "y", "a": ["b"]}
Mypy gives the following error on line 3:
Incompatible types in assignment (expression has type "Dict[str, Sequence[str]]", variable has type "Union[Dict[str, str], Dict[str, List[str]]]")
If I change the last line to s: Mytype = {"a": ["b"]}
mypy doesn't complain. However, when adding yet one more line s["a"].append("c")
leads to an error:
error: Item "str" of "Union[str, List[str]]" has no attribute "append"
How can the above mentioned be explained? How should I type a dict that has strings as keys and the values can be either strings or lists of strings?
Found this: https://github.com/python/mypy/issues/2984#issuecomment-285716826 but still not completely sure why the above mentioned happens and how should I fix it.
EDIT:
Although it's still not clear why the suggested modification Mytype = Dict[str, Union[str, List[str]]]
does not resolve the error with s['a'].append('c')
I think the TypeDict approach suggested in the comments as well as in https://stackoverflow.com/a/62862029/692695 is the way to go, so marking that approach as a solution.
See similar question at: Indicating multiple value in a Dict[] for type hints, suggested in the comments by Georgy.