title: Python Typing: declare return value type based on function argument
body: <p>Suppose I have function that takes type as argument and returns instance of that type:</p>
  
  <pre><code>def fun(t):
      return t(42)
  </code></pre>
  
  <p>Then I can call it and get objects of provided types:</p>
  
  <pre><code>fun(int)           # 42
  fun(float)         # 42.0
  fun(complex)       # (42+0j)
  fun(str)           # "42"
  fun(MyCustomType)  # something
  </code></pre>
  
  <p><sub>That list is not exhaustive, I'd like to be able to use any type with appropriate constructor.</sub></p>
  
  <p>Then, I'd like to add type hints for that function. What should be the type hint for return value of that function?</p>
  
  <hr>
  
  <p>I've tried <a href="https://mypy-play.net/?mypy=latest&amp;python=3.8&amp;gist=0022f87a7b5c21b73fd9c164f1159cd3" rel="noreferrer">using simply <code>t</code></a>, as <code>t</code> is a type:</p>
  
  <pre><code>def fun(t: type) -&gt; t:
      return t(42)
  </code></pre>
  
  <p>but that doesn't work:</p>
  
  <blockquote>
    <p>main.py:1: error: Name 't' is not defined</p>
  </blockquote>
  
  <hr>
  
  <p><a href="https://stackoverflow.com/a/48353024/226648">This answer</a> suggests <a href="https://mypy-play.net/?mypy=latest&amp;python=3.8&amp;gist=dd8aa963f4468256209817f1b71d32c6" rel="noreferrer">using a <code>TypeVar</code></a>:</p>
  
  <pre><code>from typing import TypeVar
  
  T = TypeVar("T")
  
  def fun(t: T) -&gt; T:
      return t(42)
  </code></pre>
  
  <p>But that doesn't seem to be right, as <code>T</code> denotes a type, so it suggests that type itself is returned, not its instance. Mypy rejects it:</p>
  
  <blockquote>
    <p>main.py:6: error: "object" not callable</p>
  </blockquote>
  
  <hr>
  
  <p><a href="https://mypy-play.net/?mypy=latest&amp;python=3.8&amp;gist=a68787d63c123a944a61bef877b9a2bc" rel="noreferrer">Using <code>Any</code></a> obviously work, but I feel it's too vague, it doesn't convey the intent:</p>
  
  <pre><code>from typing import Any
  
  def fun(t: type) -&gt; Any:
      return t(42)
  </code></pre>