title: mypy overload and contextmanager
body: <p>I am trying to implement an abstract &amp; concrete class for a file opener.</p>
  <p>However, with mypy (v0.910 on Python 3.8) I can't seem to get the typing working.
  Below is my current code, and the error messages from mypy.</p>
  <p>I also tried a few other &quot;combinations&quot; like <code>@contextmanager</code> on the abstract class, but just can't seem to find the right alchemy.</p>
  <pre class="lang-py prettyprint-override"><code>from abc import ABC, abstractmethod
  from contextlib import contextmanager
  from typing import ContextManager, Iterator, Literal, Union, overload
  
  
  class Reader:
      &quot;&quot;&quot;A file reader.&quot;&quot;&quot;
      def __init__(self, path: str) -&gt; None:
          self.path = path
  
  
  class Writer:
      &quot;&quot;&quot;A file writer.&quot;&quot;&quot;
      def __init__(self, path: str) -&gt; None:
          self.path = path
  
  
  class FileOpenerAbstract(ABC):
  
      @overload
      @abstractmethod
      def open(self,
               path: str,
               mode: Literal['r']
      ) -&gt; ContextManager[Reader]:
          ...
  
      @overload
      @abstractmethod
      def open(self,
               path: str,
               mode: Literal['x', 'w', 'a']
      ) -&gt; ContextManager[Writer]:
          ...
  
      @abstractmethod
      def open(
          self,
          path: str,
          mode: Literal['r', 'x', 'w', 'a'] = 'r'
      ) -&gt; Union[ContextManager[Reader], ContextManager[Writer]]:
          &quot;&quot;&quot;Open an archive&quot;&quot;&quot;
  
  
  class FileOpener(FileOpenerAbstract):
  
      @overload
      @contextmanager
      def open(self,
               path: str,
               mode: Literal['r']
      ) -&gt; Iterator[Reader]:
          ...
  
      @overload
      @contextmanager
      def open(self,
               path: str,
               mode: Literal['x', 'w', 'a']
      ) -&gt; Iterator[Writer]:
          ...
  
      @contextmanager
      def open(
          self,
          path: str,
          mode: Literal['r', 'x', 'w', 'a'] = 'r',
      ) -&gt; Union[Iterator[Reader], Iterator[Writer]]:
          if mode == 'r':
              yield Reader(path)
          else:
              yield Writer(path)
  
  </code></pre>
  <pre><code>$ mypy mypy_test.py
  mypy_test.py:47: error: Signature of &quot;open&quot; incompatible with supertype &quot;FileOpenerAbstract&quot;
  mypy_test.py:63: error: Overloaded function implementation cannot produce return type of signature 1
  mypy_test.py:63: error: Overloaded function implementation cannot produce return type of signature 2
  mypy_test.py:64: error: Signature of &quot;open&quot; incompatible with supertype &quot;FileOpenerAbstract&quot;
  Found 4 errors in 1 file (checked 1 source file)
  </code></pre>
  <p>any pointers on how to get this to work?</p>
  <p>(note autocompletion with VS Code's Pylance, seems to work with this code, so I'm not sure if it is a limitation of mypy? But then again pylint also has issues with interpreting it)</p>