title: Return None from python function annotated with mypy, multiple return types
body: <p>I come from a Typescript background. I'm bringing static type checking into a python project I'm working on (using mypy).</p>
  
  <p>In Typescript, it is valid to return null from a function that is annotated to return something else, i.e. a string:</p>
  
  <pre class="lang-typescript prettyprint-override"><code>function test(flag: boolean): string {
      if(flag) {
          return 'success';
      } else {
          return null;
      }
  }
  </code></pre>
  
  <p>It is also valid to annotate your function to have multiple potential return types, i.e. string or boolean:</p>
  
  <pre class="lang-typescript prettyprint-override"><code>function test(flag: boolean): string | boolean {
      if(flag) {
          return 'success';
      } else {
          return false;
      }
  }
  </code></pre>
  
  <p>But, in python using mypy, I'm disallowed from returning None from a function that is annotated to return <code>str</code>.</p>
  
  <pre class="lang-python prettyprint-override"><code>def test(flag: bool) -&gt; str:
      if flag:
          return 'success'
      else:
          return None
          # [mypy] error:Incompatible return value type (got "None", expected "str")
  </code></pre>
  
  <p>Furthermore, I don't see a way to annotate multiple return types, i.e. <code>str | None</code>. </p>
  
  <p>How should I approach something like this using mypy? Functions that return None from the error state are all over my codebase.</p>