title: Is there any way for mypy to support instance checking from within a function?
body: <p>Is there any way for <code>mypy</code> to support instance checking from <em>within</em> a function?</p>
  
  <p>I am aware of the <code>isinstance</code> support for <a href="https://mypy.readthedocs.io/en/latest/common_issues.html#complex-type-tests" rel="nofollow noreferrer">complex type checking</a>. I have the following module as minimal example for illustration purposes:</p>
  
  <pre><code>from typing import Any, Union
  
  def is_str(argument: Any) -&gt; bool:
      return True if isinstance(argument, str) else False
  
  def my_string_function(input_str: str):
      print(input_str)
  
  def my_string_and_int_function(argument: Union[str, int]):
  
      if is_str(argument):
          my_string_function(argument)
  
      if isinstance(argument, str):
          my_string_function(argument)
  </code></pre>
  
  <p>Now if I run mypy I get the following error for the <em>first</em> (not the second!) function call of <code>my_string_function</code> from within the <code>my_string_and_int_function</code>:</p>
  
  <blockquote>
    <p>error: Argument 1 to "my_string_function" has incompatible type "Union[str, int]"; expected "str"</p>
  </blockquote>
  
  <p>Maybe this is more appropriate for the mypy issue tracker but as I am unsure I thought of posting it here first.</p>