title: How to type a generic function with namedtuples?
body: <p>I have a wrapper class for a database cursor like this:</p>
  <pre><code>namedtuple_def = TypeVar(&quot;namedtuple_def&quot;)
  
  class Wrapper:
      def fetchall(defn: Optional[namedtuple_def] = None) -&gt; List[namedtuple_def]:
          return self.cursor.fetchall()
  </code></pre>
  <p>The underlying cursor always returns namedtuples, but the nametuple returned depends on the query run. So I'm trying to get it to take a nametuple type as an unused parameter to hint exactly what types are getting returned. If you call it without the defn parameter the return type is partially erased, but if you pass the equivalent namedtuple class your query results have fully specified types.</p>
  <p>My problem with the code above is that the typechecker thinks I'm returning the type variable/class for the namedtuple argument, not the instantiated namedtuples - or in other words, <code>List[Type[my_namedtuple]]</code> instead of <code>List[my_namedtuple]</code>. How do I fix the type signature?</p>
  <p>n.b. I am using the PyCharm typechecker, not MyPy, but I don't expect them to vary here.</p>