torch.jit.Future type annotation

cnapun: <p>Is there any way to give a type hint to the output of <code>torch.jit._wait</code>?</p>
<p>The below example fails to compile to torchscript</p>
<pre><code class="lang-auto">import torch
from typing import List

def process(i: int) -&gt; int:
    return i + 1

@torch.jit.script
def process_many(l: List[int]) -&gt; List[int]:
    futs: List[torch.jit.Future] = []
    out: List[int] = []

    for v in l:
        futs.append(torch.jit._fork(process, v))

    for f in futs:
        out.append(torch.jit._wait(f))

    return out
</code></pre>
<p>with the error</p>
<pre><code class="lang-auto">  File "/usr/local/lib/python3.7/site-packages/torch/jit/__init__.py", line 1281, in script
    fn = torch._C._jit_script_compile(qualified_name, ast, _rcb, get_default_args(obj))
RuntimeError:
Unknown type name 'torch.jit.Future':
  File "./demo.py", line 10
@torch.jit.script
def process_many(l: List[int]) -&gt; List[int]:
    futs: List[torch.jit.Future] = []
               ~~~~~~~~~~~~~~~~ &lt;--- HERE
    out: List[int] = []
</code></pre>
<p>However, if i change the for loop to a list comprehension, it compiles fine</p>
<pre><code class="lang-auto">@torch.jit.script
def process_many(l: List[int]) -&gt; List[int]:
    futs = [torch.jit._fork(process, v) for v in l]
    out: List[int] = []

    for f in futs:
        out.append(torch.jit._wait(f))

    return out

# &gt;&gt;&gt; print(process_many([0, 3, 5])
# [1, 4, 6]
</code></pre>
<p>What haven’t been able to figure out is if <code>torch.jit.Future</code> is the correct type annotation for me to be using here. For now, it seems possible to work around this just using a list comprehension, but it could get ugly if the logic becomes more complex.</p>
<p>I’m on 1.4.0, if it makes any difference</p>

driazati: <p>Our future support is really only half done (it’s still an internal API, hence the <code>_</code> at the beginning of <code>fork</code> and <code>wait</code>) so I don’t think there’s a way to do this without the list comprehension you mentioned. Could you <a href="https://github.com/pytorch/pytorch/issues/new/choose">file a bug report here</a>?</p>

cnapun: <p>Thanks! just discovered that there seems to be a bug report out for this already<br>
<aside class="onebox githubissue">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/issues/26578" target="_blank" rel="nofollow noopener">github.com/pytorch/pytorch</a>
  </header>
  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewbox="0 0 14 16" aria-hidden="true"><path d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/issues/26578" target="_blank" rel="nofollow noopener">[jit] Fix future type annotation in python</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2019-09-20" data-time="23:35:55" data-timezone="UTC">11:35PM - 20 Sep 19 UTC</span>
      </div>


      <div class="user">
        <a href="https://github.com/wanchaol" target="_blank" rel="nofollow noopener">
          <img alt="wanchaol" src="https://avatars2.githubusercontent.com/u/9443650?v=4" class="onebox-avatar-inline" width="20" height="20">
          wanchaol
        </a>
      </div>
    </div>
  </div>
</div>

<div class="github-row">
  <p class="github-content">We should fix the Future type annotation to make it also work in python, fork will immediately execute the function single...</p>
</div>

<div class="labels">
    <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">high priority</span>
    <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">jit</span>
    <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">jit-backlog</span>
    <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">triaged</span>
</div>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>