title: How to cast a typing.Union to one of its subtypes in Python?
body: <p>I’m using Python 3.6.1, mypy, and the typing module. I created two custom types, <code>Foo</code> and <code>Bar</code>, and then used them in a dict I return from a function. The dict is described as mapping <code>str</code> to a <code>Union</code> of <code>Foo</code> and <code>Bar</code>. Then I want to use values from this dict in a function that names only one argument each:</p>
  
  <pre><code>from typing import Dict, Union, NewType
  
  Foo = NewType("Foo", str)
  Bar = NewType("Bar", int)
  
  def get_data() -&gt; Dict[str, Union[Foo, Bar]]:
      return {"foo": Foo("one"), "bar": Bar(2)}
  
  def process(foo_value: Foo, bar_value: Bar) -&gt; None:
      pass
  
  d = get_data()
  </code></pre>
  
  <p>I tried using the values as-is:</p>
  
  <pre><code>process(d["foo"], d["bar"])
  # typing-union.py:15: error: Argument 1 to "process" has incompatible type "Union[Foo, Bar]"; expected "Foo"
  # typing-union.py:15: error: Argument 2 to "process" has incompatible type "Union[Foo, Bar]"; expected "Bar"
  </code></pre>
  
  <p>Or using the types:</p>
  
  <pre><code>process(Foo(d["foo"]), Bar(d["bar"]))
  # typing-union.py:20: error: Argument 1 to "Foo" has incompatible type "Union[Foo, Bar]"; expected "str"
  # typing-union.py:20: error: Argument 1 to "Bar" has incompatible type "Union[Foo, Bar]"; expected "int"
  </code></pre>
  
  <p>How do I cast the <code>Union</code> to one of its subtypes?</p>