title: Mypy: Generic container with some methods only valid if extra protocols apply
body: <p>In mypy, how would you specify that a type <code>Generic</code> over <code>T</code> has methods that are only valid if <code>T</code> meets certain conditions?</p>
  <p>For example, if we made a custom collection class with a <code>min</code> method, returning the smallest element in the collection:</p>
  <pre class="lang-py prettyprint-override"><code>from typing import Generic, TypeVar
  
  T = TypeVar(&quot;T&quot;)
  
  class MyCollection(Generic[T]):
      
      def __init__(self, some_list: List[T]):
          self._storage = some_list
  
      def min(self) -&gt; T: # This requires that T implements __lt__
          &quot;Get the smallest element in the collection&quot;
          return min(self._storage)     
  </code></pre>
  <p>How can you tell the type system that calling <code>min</code> on <code>MyCollection</code> of <code>T</code> is only allowed if <code>T</code> implements <code>__lt__</code>?</p>
  <p>So basically I'd like to have some methods of a generic container only be valid if extra protocols are met.</p>
  <p>-- Useful links --</p>
  <p>You can see from the <a href="https://github.com/python/typeshed/blob/d2a7889fe0caafaa39758d332f593d537aa4dd80/stdlib/3/builtins.pyi#L1033" rel="nofollow noreferrer">typehints in the standardlib for min</a> that they've defined a protocol for enforcing <code>__lt__</code> is implemented</p>
  <pre class="lang-py prettyprint-override"><code>class SupportsLessThan(Protocol):
      def __lt__(self, __other: Any) -&gt; bool: ...
  
  SupportsLessThanT = TypeVar(&quot;SupportsLessThanT&quot;, bound=SupportsLessThan)  # noqa: Y001
  </code></pre>