title: How to type hint a return an instance of a subclass from a list of superclass in Python
body: <p>If I have a class that stores a list of superclasses, and I have a method that filters out a specific subclass types, how can I set the type hint to say that the method returns a list of the subclass?</p>
  <p>Example:</p>
  <pre><code>from typing import List
  
  
  class Color:
      name: str
  
      def __init__(self, name):
          self.name = name
  
  
  class Green(Color):
      def __init__(self):
          super().__init__('green')
  
      def do_a_green(self):
          pass
  
  
  class Blue(Color):
      def __init__(self):
          super().__init__('blue')
  
      def do_a_blue(self):
          pass
  
  
  class ColorList:
      _color_list: List[Color]
  
      def get_blue_colors(self) -&gt; List[Blue]:
          return [color for color in self._color_list
                  if color.name == 'blue'] # &lt;- error: List comprehension has
                                           # incompatible type List[Color]; expected List[Blue]
  
  </code></pre>