title: How to properly annotate a ContextManager in PyCharm?
body: <p>How can I annotate the yield type of a <code>contextmanager</code> in PyCharm so that it properly guesses the type of the value used in the <code>with</code> clauses - just as it guesses that the <code>f</code> created in <code>with open(...) as f</code> is a file?</p>
  <p>For example, I have a context manager like this:</p>
  <pre class="lang-py prettyprint-override"><code>@contextlib.contextmanager
  def temp_borders_file(geometry: GEOSGeometry, name='borders.json'):
      with TemporaryDirectory() as temp_dir:
          borders_file = Path(dir) / name
          with borders_file.open('w+') as f:
              f.write(geometry.json)
          yield borders_file
  
  with temp_borders_file(my_geom) as borders_f:
      do_some_code_with(borders_f...)
  </code></pre>
  <p>How do I let PyCharm know that every <code>borders_f</code> created like this is a <code>pathlib.Path</code> (and thus enable the autocompletion for the <code>Path</code> methods on <code>border_f</code>)? Of course, I can make a comment like <code># type: Path</code> after every <code>with</code> statement, but it seems that this can be done by properly annotating <code>temp_border_file</code>.</p>
  <p>I tried <code>Path</code>, <code>typing.Iterator[Path]</code> and <code>typing.Generator[Path, None, None]</code> as the return type of <code>temp_border_file</code>, as well as adding <code># type: Path</code> on <code>borders_file</code> within the context manager's code, but it seems like it doesn't help.</p>