title: Dependent types and polymorphism in Python with mypy
body: <p>For the following example, <code>mypy</code> returns an error:</p>
  
  <blockquote>
    <p>error: Incompatible types in assignment (expression has type "A",
    variable has type "A1")</p>
  </blockquote>
  
  <pre class="lang-py prettyprint-override"><code>from typing import Type
  
  class A:
      pass
  
  class A1(A):
      pass
  
  class A2(A):
      pass
  
  def fun(A_type: Type[A]) -&gt; A:
      if A_type == A1:
          return A1()
      else:
          return A2()
  
  a1: A1 = fun(A1)
  </code></pre>
  
  <p>What I would ideally like to do is to enforce a dependency in the signature of <code>fun</code>:</p>
  
  <pre class="lang-py prettyprint-override"><code>def fun(A_type: Type[A]) -&gt; A_type
  </code></pre>
  
  <p>Is this possible; if not, what is recommended (note: I want this to work for as yet undefined sub-classes of <code>A</code>, so I don't think I can use the <code>overload</code> decorator)?  Is my best option just to use <code>cast</code>?</p>