title: How to create a collection class that is able to manage and create objects of a certain class and its subclasses with proper type hints
body: <p>I have a class <code>Thing</code> that represents some thing and a class <code>ThingCollection</code> that manages a collection of such things. Like this:</p>
  <pre class="lang-py prettyprint-override"><code>class Thing:
      def __init__(self, a: int) -&gt; None:
          self.a = a
  
  
  class ThingCollection:
      def __init__(self) -&gt; None:
          self.things = []
  
      # ...
  
      def load(self, things_to_load) -&gt; None:
          # Simplified, would maybe load from a file
          self.things = [Thing(**x) for x in things_to_load]
  </code></pre>
  <p>This can be used like this:</p>
  <pre class="lang-py prettyprint-override"><code>coll = ThingCollection()
  coll.load([{&quot;a&quot;: 12}])
  </code></pre>
  <p>Now, I'd like to have subclasses of <code>Thing</code>s and manage those with the same <code>ThingCollection</code>. Like this:</p>
  <pre class="lang-py prettyprint-override"><code>class ExtendedThing(Thing):
      def __init__(self, a: int, b: int) -&gt; None:
          super().__init__(a)
          self.b = b
  
  
  class ThingCollection2:
      def __init__(self) -&gt; None:
          self.things = []
  
      # ...
  
      def load(self, things_to_load, which_class) -&gt; None:
          self.things = [which_class(**x) for x in things_to_load]
  </code></pre>
  <p>This works fine:</p>
  <pre class="lang-py prettyprint-override"><code>coll2 = ThingCollection2()
  coll2.load([{&quot;a&quot;: 12, &quot;b&quot;: 13}], ExtendedThing)
  print(coll2.things[0].b)
  </code></pre>
  <p>However, if I want to add type hints, the type checker complains. I.e., changing the signature of <code>load</code> to:</p>
  <pre class="lang-py prettyprint-override"><code>    def load(self, things_to_load, which_class: Type[Thing]) -&gt; None:
  </code></pre>
  <p>will lead to a complaint of <code>Cannot access member &quot;b&quot; for type &quot;Thing&quot; Member &quot;b&quot; is unknown</code> for the print statement above.</p>
  <p>My questions:</p>
  <ol>
  <li>What are alternative approaches that resolve this problem? One would be to also subclass the <code>ThingCollection</code> to, e.g., <code>ExtendedThingCollection</code>, but that feels clumsy and redundant.</li>
  <li>What are pros and cons of the different approaches other than related to the type hint problem above?</li>
  <li>What would be the right terminology to ask the question I'm asking? Are there design patterns that are relevant with regard to this question?</li>
  </ol>