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