<h2 dir="auto">🐛 Bug</h2>
<p dir="auto">The following error</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="RuntimeError: 
Could not export Python function call 'type'"><pre class="notranslate"><code class="notranslate">RuntimeError: 
Could not export Python function call 'type'
</code></pre></div>
<p dir="auto">is raised only on <code class="notranslate">save</code> and in a specific case. See below.</p>
<h2 dir="auto">To Reproduce</h2>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import torch


def check_input(img: torch.Tensor) -&gt; bool:
    return isinstance(img, torch.Tensor)


def foo(img: torch.Tensor) -&gt; torch.Tensor:
    if check_input(img):
        return img
    raise TypeError(&quot;Unexpected input type. Got {}&quot;.format(type(img)))


def bar(img: torch.Tensor) -&gt; torch.Tensor:
    if isinstance(img, torch.Tensor):
        return img
    raise TypeError(&quot;Unexpected input type. Got {}&quot;.format(type(img)))


if __name__ == &quot;__main__&quot;:

    t = torch.rand(3, 12, 12)
    s_bar = torch.jit.script(bar)

    assert bar(t).equal(s_bar(t))
    s_bar.save(&quot;test_bar.pt&quot;)

    t = torch.rand(3, 12, 12)
    s_foo = torch.jit.script(foo)

    assert foo(t).equal(s_foo(t))
    s_foo.save(&quot;test_foo.pt&quot;)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">torch</span>


<span class="pl-k">def</span> <span class="pl-en">check_input</span>(<span class="pl-s1">img</span>: <span class="pl-s1">torch</span>.<span class="pl-v">Tensor</span>) <span class="pl-c1">-&gt;</span> <span class="pl-s1">bool</span>:
    <span class="pl-k">return</span> <span class="pl-en">isinstance</span>(<span class="pl-s1">img</span>, <span class="pl-s1">torch</span>.<span class="pl-v">Tensor</span>)


<span class="pl-k">def</span> <span class="pl-en">foo</span>(<span class="pl-s1">img</span>: <span class="pl-s1">torch</span>.<span class="pl-v">Tensor</span>) <span class="pl-c1">-&gt;</span> <span class="pl-s1">torch</span>.<span class="pl-v">Tensor</span>:
    <span class="pl-k">if</span> <span class="pl-en">check_input</span>(<span class="pl-s1">img</span>):
        <span class="pl-k">return</span> <span class="pl-s1">img</span>
    <span class="pl-k">raise</span> <span class="pl-v">TypeError</span>(<span class="pl-s">"Unexpected input type. Got {}"</span>.<span class="pl-en">format</span>(<span class="pl-en">type</span>(<span class="pl-s1">img</span>)))


<span class="pl-k">def</span> <span class="pl-en">bar</span>(<span class="pl-s1">img</span>: <span class="pl-s1">torch</span>.<span class="pl-v">Tensor</span>) <span class="pl-c1">-&gt;</span> <span class="pl-s1">torch</span>.<span class="pl-v">Tensor</span>:
    <span class="pl-k">if</span> <span class="pl-en">isinstance</span>(<span class="pl-s1">img</span>, <span class="pl-s1">torch</span>.<span class="pl-v">Tensor</span>):
        <span class="pl-k">return</span> <span class="pl-s1">img</span>
    <span class="pl-k">raise</span> <span class="pl-v">TypeError</span>(<span class="pl-s">"Unexpected input type. Got {}"</span>.<span class="pl-en">format</span>(<span class="pl-en">type</span>(<span class="pl-s1">img</span>)))


<span class="pl-k">if</span> <span class="pl-s1">__name__</span> <span class="pl-c1">==</span> <span class="pl-s">"__main__"</span>:

    <span class="pl-s1">t</span> <span class="pl-c1">=</span> <span class="pl-s1">torch</span>.<span class="pl-en">rand</span>(<span class="pl-c1">3</span>, <span class="pl-c1">12</span>, <span class="pl-c1">12</span>)
    <span class="pl-s1">s_bar</span> <span class="pl-c1">=</span> <span class="pl-s1">torch</span>.<span class="pl-s1">jit</span>.<span class="pl-en">script</span>(<span class="pl-s1">bar</span>)

    <span class="pl-k">assert</span> <span class="pl-en">bar</span>(<span class="pl-s1">t</span>).<span class="pl-en">equal</span>(<span class="pl-en">s_bar</span>(<span class="pl-s1">t</span>))
    <span class="pl-s1">s_bar</span>.<span class="pl-en">save</span>(<span class="pl-s">"test_bar.pt"</span>)

    <span class="pl-s1">t</span> <span class="pl-c1">=</span> <span class="pl-s1">torch</span>.<span class="pl-en">rand</span>(<span class="pl-c1">3</span>, <span class="pl-c1">12</span>, <span class="pl-c1">12</span>)
    <span class="pl-s1">s_foo</span> <span class="pl-c1">=</span> <span class="pl-s1">torch</span>.<span class="pl-s1">jit</span>.<span class="pl-en">script</span>(<span class="pl-s1">foo</span>)

    <span class="pl-k">assert</span> <span class="pl-en">foo</span>(<span class="pl-s1">t</span>).<span class="pl-en">equal</span>(<span class="pl-en">s_foo</span>(<span class="pl-s1">t</span>))
    <span class="pl-s1">s_foo</span>.<span class="pl-en">save</span>(<span class="pl-s">"test_foo.pt"</span>)</pre></div>
<p dir="auto">and output</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;test_type.py&quot;, line 32, in &lt;module&gt;
    s_foo.save(&quot;test_foo.pt&quot;)
RuntimeError: 
Could not export Python function call 'type'. Remove calls to Python functions before export. Did you forget to add @script or @script_method annotation? If this is a nn.ModuleList, add it to __constants__:
  File &quot;test_type.py&quot;, line 11
    if check_input(img):
        return img
    raise TypeError(&quot;Unexpected input type. Got {}&quot;.format(type(img)))
                                                           ~~~~ &lt;--- HERE"><pre class="notranslate"><code class="notranslate">Traceback (most recent call last):
  File "test_type.py", line 32, in &lt;module&gt;
    s_foo.save("test_foo.pt")
RuntimeError: 
Could not export Python function call 'type'. Remove calls to Python functions before export. Did you forget to add @script or @script_method annotation? If this is a nn.ModuleList, add it to __constants__:
  File "test_type.py", line 11
    if check_input(img):
        return img
    raise TypeError("Unexpected input type. Got {}".format(type(img)))
                                                           ~~~~ &lt;--- HERE
</code></pre></div>
<h2 dir="auto">Environment</h2>
<p dir="auto">Please copy and paste the output from our<br>
<a href="https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py" rel="nofollow">environment collection script</a><br>
(or fill out the checklist below manually).</p>
<p dir="auto">You can get the script and run it with:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="wget https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py
# For security purposes, please check the contents of collect_env.py before running it.
python collect_env.py"><pre class="notranslate"><code class="notranslate">wget https://raw.githubusercontent.com/pytorch/pytorch/master/torch/utils/collect_env.py
# For security purposes, please check the contents of collect_env.py before running it.
python collect_env.py
</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Versions of relevant libraries:
[pip3] numpy==1.19.2
[pip3] pytorch-sphinx-theme==0.0.24
[pip3] torch==1.7.0.dev20200928
[pip3] torchvision==0.8.0a0+6f75883
[conda] blas                      1.0                         mkl  
[conda] cudatoolkit               10.1.243             h6bb024c_0  
[conda] mkl                       2020.2                      256  
[conda] mkl-service               2.3.0            py37he904b0f_0  
[conda] mkl_fft                   1.2.0            py37h23d657b_0  
[conda] mkl_random                1.1.1            py37h0573a6f_0  
[conda] numpy                     1.19.2                   pypi_0    pypi
[conda] pytorch                   1.7.0.dev20200928 py3.7_cuda10.1.243_cudnn7.6.3_0    pytorch-nightly
[conda] pytorch-sphinx-theme      0.0.24                    dev_0    &lt;develop&gt;
[conda] torchvision               0.8.0a0+6f75883           dev_0    &lt;develop&gt;"><pre class="notranslate"><code class="notranslate">Versions of relevant libraries:
[pip3] numpy==1.19.2
[pip3] pytorch-sphinx-theme==0.0.24
[pip3] torch==1.7.0.dev20200928
[pip3] torchvision==0.8.0a0+6f75883
[conda] blas                      1.0                         mkl  
[conda] cudatoolkit               10.1.243             h6bb024c_0  
[conda] mkl                       2020.2                      256  
[conda] mkl-service               2.3.0            py37he904b0f_0  
[conda] mkl_fft                   1.2.0            py37h23d657b_0  
[conda] mkl_random                1.1.1            py37h0573a6f_0  
[conda] numpy                     1.19.2                   pypi_0    pypi
[conda] pytorch                   1.7.0.dev20200928 py3.7_cuda10.1.243_cudnn7.6.3_0    pytorch-nightly
[conda] pytorch-sphinx-theme      0.0.24                    dev_0    &lt;develop&gt;
[conda] torchvision               0.8.0a0+6f75883           dev_0    &lt;develop&gt;
</code></pre></div>
<h2 dir="auto">Additional context</h2>
<p dir="auto">Torchvision transforms scripting</p>
<p dir="auto">cc <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/gmagogsfm/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/gmagogsfm">@gmagogsfm</a></p>
<h2 dir="auto"><g-emoji class="g-emoji" alias="rocket" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f680.png">🚀</g-emoji> Feature</h2>
<p dir="auto">Allow using of type() to get type string of a Python object in TorchScript</p>
<h2 dir="auto">Motivation</h2>
<p dir="auto">type() is commonly used when creating meaningful error messages, especially so after we <a href="https://github.com/pytorch/pytorch/pull/41907" data-hovercard-type="pull_request" data-hovercard-url="/pytorch/pytorch/pull/41907/hovercard">support raising exception with custom error messages</a>.</p>
<h2 dir="auto">Pitch</h2>
<p dir="auto">type() should be supported and return a string in TorchScript.</p>
<h2 dir="auto">Example</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content=" cat type_error_msg.py                                                                                                                                                                                       [ruby-2.5.1p57]
import torch


@torch.jit.script
def fn(cond: torch.Tensor):
    if bool(cond):
        raise RuntimeError(&quot;incorrect type:&quot; + str(type(cond)))

fn(torch.Tensor(1))"><pre class="notranslate"><code class="notranslate"> cat type_error_msg.py                                                                                                                                                                                       [ruby-2.5.1p57]
import torch


@torch.jit.script
def fn(cond: torch.Tensor):
    if bool(cond):
        raise RuntimeError("incorrect type:" + str(type(cond)))

fn(torch.Tensor(1))
</code></pre></div>
<p dir="auto">Currently this example errors out:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Traceback of TorchScript (most recent call last):
  File &quot;type_error_msg.py&quot;, line 7, in fn
def fn(cond: torch.Tensor):
    if bool(cond):
        raise RuntimeError(&quot;incorrect type:&quot; + str(type(cond)))
                                                   ~~~~ &lt;--- HERE
RuntimeError:  expected value of type Tensor for return value but instead got value of type type.
Value: &lt;class 'torch.Tensor'&gt;"><pre class="notranslate"><code class="notranslate">Traceback of TorchScript (most recent call last):
  File "type_error_msg.py", line 7, in fn
def fn(cond: torch.Tensor):
    if bool(cond):
        raise RuntimeError("incorrect type:" + str(type(cond)))
                                                   ~~~~ &lt;--- HERE
RuntimeError:  expected value of type Tensor for return value but instead got value of type type.
Value: &lt;class 'torch.Tensor'&gt;
</code></pre></div>
<p dir="auto">cc <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/suo/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/suo">@suo</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/gmagogsfm/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/gmagogsfm">@gmagogsfm</a></p>