<h2 dir="auto"><g-emoji class="g-emoji" alias="bug" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f41b.png">🐛</g-emoji> Bug</h2>
<p dir="auto">Torchscript seems to require comments (in a function) to be indented otherwise it complains with an error message that is not very explicit.</p>
<p dir="auto">However when you comment stuff (e.g. on a jupyter notebook) the comment symbol can be placed at the beginning of the line. It can be confusing.</p>
<h2 dir="auto">To Reproduce</h2>
<p dir="auto">Steps to reproduce the behavior:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import torch
import torch.nn as nn

class M(nn.Module):
    def __init__(self):
        super().__init__()
    def forward(inputs):
#         some comment
        return inputs

M_instance = M()
torch.jit.script(M_instance)"><pre class="notranslate"><code class="notranslate">import torch
import torch.nn as nn

class M(nn.Module):
    def __init__(self):
        super().__init__()
    def forward(inputs):
#         some comment
        return inputs

M_instance = M()
torch.jit.script(M_instance)
</code></pre></div>
<p dir="auto">leads to:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="File &quot;/***/python3.7/ast.py&quot;, line 35, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)

  File &quot;&lt;unknown&gt;&quot;, line 1
    def forward(inputs):
    ^
IndentationError: unexpected indent"><pre class="notranslate"><code class="notranslate">File "/***/python3.7/ast.py", line 35, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)

  File "&lt;unknown&gt;", line 1
    def forward(inputs):
    ^
IndentationError: unexpected indent
</code></pre></div>
<p dir="auto">However if I indent my comment it works:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import torch

class M(nn.Module):
    def __init__(self):
        super().__init__()
    def forward(inputs):
        #         some comment
        return inputs

M_instance = M()
torch.jit.script(M_instance)

&gt; RecursiveScriptModule(original_name=M)
"><pre class="notranslate"><code class="notranslate">import torch

class M(nn.Module):
    def __init__(self):
        super().__init__()
    def forward(inputs):
        #         some comment
        return inputs

M_instance = M()
torch.jit.script(M_instance)

&gt; RecursiveScriptModule(original_name=M)

</code></pre></div>
<h2 dir="auto">Expected behavior</h2>
<p dir="auto">I would expect it to simply ignore all lines where the first character is. #</p>
<h2 dir="auto">Environment</h2>
<ul dir="auto">
<li>PyTorch Version (e.g., 1.0): 1.5.0a0-fb</li>
</ul>
<h2 dir="auto">Additional context</h2>
<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></p>
<p dir="auto">This can cause issues when you have comments at a lower indentation than the rest of the code:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import torch

class Foo(torch.nn.Module):
    def forward(self, x):
        x = torch.neg(x)
# foo foo foo i have a comment at the wrong indent
        return x

torch.jit.script(Foo())"><pre class="notranslate"><code class="notranslate">import torch

class Foo(torch.nn.Module):
    def forward(self, x):
        x = torch.neg(x)
# foo foo foo i have a comment at the wrong indent
        return x

torch.jit.script(Foo())
</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;ought.py&quot;, line 9, in &lt;module&gt;
    torch.jit.script(Foo())
  File &quot;/home/jamesreed/pytorch/torch/jit/__init__.py&quot;, line 1159, in script
    return torch.jit.torch.jit._recursive.recursive_script(obj)
  File &quot;/home/jamesreed/pytorch/torch/jit/_recursive.py&quot;, line 166, in recursive_script
    stubs = list(map(make_stub, filtered_methods))
  File &quot;/home/jamesreed/pytorch/torch/jit/_recursive.py&quot;, line 163, in make_stub
    return torch.jit.script_method(func, _jit_internal.createResolutionCallbackFromClosure(func))
  File &quot;/home/jamesreed/pytorch/torch/jit/__init__.py&quot;, line 1224, in script_method
    ast = get_jit_def(fn, self_name=&quot;ScriptModule&quot;)
  File &quot;/home/jamesreed/pytorch/torch/jit/frontend.py&quot;, line 164, in get_jit_def
    py_ast = ast.parse(dedent_src)
  File &quot;/home/jamesreed/miniconda3/lib/python3.6/ast.py&quot;, line 35, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)
  File &quot;&lt;unknown&gt;&quot;, line 1
    def forward(self, x):
    ^
IndentationError: unexpected indent"><pre class="notranslate"><code class="notranslate">Traceback (most recent call last):
  File "ought.py", line 9, in &lt;module&gt;
    torch.jit.script(Foo())
  File "/home/jamesreed/pytorch/torch/jit/__init__.py", line 1159, in script
    return torch.jit.torch.jit._recursive.recursive_script(obj)
  File "/home/jamesreed/pytorch/torch/jit/_recursive.py", line 166, in recursive_script
    stubs = list(map(make_stub, filtered_methods))
  File "/home/jamesreed/pytorch/torch/jit/_recursive.py", line 163, in make_stub
    return torch.jit.script_method(func, _jit_internal.createResolutionCallbackFromClosure(func))
  File "/home/jamesreed/pytorch/torch/jit/__init__.py", line 1224, in script_method
    ast = get_jit_def(fn, self_name="ScriptModule")
  File "/home/jamesreed/pytorch/torch/jit/frontend.py", line 164, in get_jit_def
    py_ast = ast.parse(dedent_src)
  File "/home/jamesreed/miniconda3/lib/python3.6/ast.py", line 35, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)
  File "&lt;unknown&gt;", line 1
    def forward(self, x):
    ^
IndentationError: unexpected indent
</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></p>