title: Generic protocols: mypy error: Argument 1 has incompatible type ...; expected
body: <p>I'm trying to implement a generic Protocol. My intent is to have a Widget[key_type, value_type] protocol with a simple getter.  Mypy complained about <code>Protocol[K, T]</code> so that became <code>Protocol[K_co, T_co]</code>. I've already stripped out all the other constraints, but I can't even get the most basic situation, <code>widg0: Widget[Any, Any] = ActualWidget()</code>, to work. ActualWidget.get should be totally compatible with <code>get(self, key: K) -&gt; Any</code>, which makes me think I'm using the generics/protocol wrong in some way, or mypy just can't handle this.</p>
  <p>command/error from mypy:</p>
  <pre><code>$ mypy cat_example.py
  cat_example.py:34: error: Argument 1 to &quot;takes_widget&quot; has incompatible type &quot;ActualWidget&quot;; expected &quot;Widget[Any, Any]&quot;
  cat_example.py:34: note: Following member(s) of &quot;ActualWidget&quot; have conflicts:
  cat_example.py:34: note:     Expected:
  cat_example.py:34: note:         def [K] get(self, key: K) -&gt; Any
  cat_example.py:34: note:     Got:
  cat_example.py:34: note:         def get(self, key: str) -&gt; Cat
  Found 1 error in 1 file (checked 1 source file)
  </code></pre>
  <p>or alternatively, if I try to force the assignment with <code>widg0: Widget[Any, Any] = ActualWidget()</code>:</p>
  <pre><code>error: Incompatible types in assignment (expression has type &quot;ActualWidget&quot;, variable has type &quot;Widget[Any, Any]&quot;)
  </code></pre>
  <p>The full code:</p>
  <pre class="lang-py prettyprint-override"><code>from typing import Any, TypeVar
  from typing_extensions import Protocol, runtime_checkable
  
  K = TypeVar(&quot;K&quot;)  # ID/Key Type
  T = TypeVar(&quot;T&quot;)  # General type
  K_co = TypeVar(&quot;K_co&quot;, covariant=True)  # ID/Key Type or subclass
  T_co = TypeVar(&quot;T_co&quot;, covariant=True)  # General type or subclass
  K_contra = TypeVar(&quot;K_contra&quot;, contravariant=True)  # ID/Key Type or supertype
  T_contra = TypeVar(&quot;T_contra&quot;, contravariant=True)  # General type or supertype
  
  class Animal(object): ...
  
  class Cat(Animal): ...
  
  
  @runtime_checkable
  class Widget(Protocol[K_co, T_co]):
      def get(self, key: K) -&gt; T_co: ...
  
  class ActualWidget(object):
      def get(self, key: str) -&gt; Cat:
          return Cat()
  
  def takes_widget(widg: Widget):
      return widg
  
  if __name__ == '__main__':
      widg0 = ActualWidget()
      #widg0: Widget[str, Cat] = ActualWidget()
      #widg0: Widget[Any, Any] = ActualWidget()
  
      print(isinstance(widg0, Widget))
      print(isinstance({}, Widget))
      takes_widget(widg0)
  </code></pre>