Torchscript results in AttributeError: 'Call' object has no attribute 'id'

I try to TorchScript my function with @torch.jit.script decoration, which itself calls other functions from another module. But I ended up with the following error.

File “/venv/lib/python3.7/site-packages/torch/jit/init.py”, line 1551, in script
fn = torch._C._jit_script_compile(qualified_name, ast, _rcb, get_default_args(obj))
File “/venv/lib/python3.7/site-packages/torch/jit/_recursive.py”, line 583, in try_compile_fn
return torch.jit.script(fn, _rcb=rcb)
File “/venv/lib/python3.7/site-packages/torch/jit/init.py”, line 1547, in script
ast = get_jit_def(obj, obj.name)
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 185, in get_jit_def
return build_def(ctx, fn_def, type_line, def_name, self_name=self_name)
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 219, in build_def
build_stmts(ctx, body))
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 126, in build_stmts
stmts = [build_stmt(ctx, s) for s in stmts]
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 126, in
stmts = [build_stmt(ctx, s) for s in stmts]
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 193, in call
return method(ctx, node)
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 384, in build_If
build_stmts(ctx, stmt.body),
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 126, in build_stmts
stmts = [build_stmt(ctx, s) for s in stmts]
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 126, in
stmts = [build_stmt(ctx, s) for s in stmts]
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 193, in call
return method(ctx, node)
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 384, in build_If
build_stmts(ctx, stmt.body),
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 126, in build_stmts
stmts = [build_stmt(ctx, s) for s in stmts]
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 126, in
stmts = [build_stmt(ctx, s) for s in stmts]
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 193, in call
return method(ctx, node)
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 385, in build_If
build_stmts(ctx, stmt.orelse))
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 126, in build_stmts
stmts = [build_stmt(ctx, s) for s in stmts]
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 126, in
stmts = [build_stmt(ctx, s) for s in stmts]
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 193, in call
return method(ctx, node)
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 384, in build_If
build_stmts(ctx, stmt.body),
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 126, in build_stmts
stmts = [build_stmt(ctx, s) for s in stmts]
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 126, in
stmts = [build_stmt(ctx, s) for s in stmts]
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 193, in call
return method(ctx, node)
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 413, in build_With
return With(r, build_withitems(ctx, stmt.items), build_stmts(ctx, stmt.body))
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 121, in build_withitems
items = [build_withitem(ctx, i) for i in items]
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 121, in
items = [build_withitem(ctx, i) for i in items]
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 193, in call
return method(ctx, node)
File “/venv/lib/python3.7/site-packages/torch/jit/frontend.py”, line 282, in build_withitem
end = start + len(item.context_expr.id)
AttributeError: ‘Call’ object has no attribute ‘id’

The qualified_name in torch._C._jit_script_compile(qualified_name, ast, _rcb, get_default_args(obj)) is referenced to a simple function which does not call any PyTorch module.
I use PyTorch 1.6.0 with Python 3.7. Any idea?

It seems changing the WithItemBuilder class from (PyTorch 1.6.0):

class WithItemBuilder(Builder):
    @staticmethod
    def build_withitem(ctx, item):
        lineno = item.context_expr.lineno
        start = item.context_expr.col_offset
        op_vars = item.optional_vars
        if op_vars:
            end = op_vars.col_offset + len(op_vars.id)
        else:
            end = start + len(item.context_expr.id)

        r = ctx.make_range(lineno, start, end)

        return WithItem(r, build_expr(ctx, item.context_expr), build_expr(ctx, op_vars) if op_vars else None)

to (PyTorch master branch)

class WithItemBuilder(Builder):
    @staticmethod
    def build_withitem(ctx, item):
        lineno = item.context_expr.lineno
        start = item.context_expr.col_offset
        end = start + len(pretty_node_names[ast.With])
        op_vars = item.optional_vars
        r = ctx.make_range(lineno, start, end)

        return WithItem(r, build_expr(ctx, item.context_expr), build_expr(ctx, op_vars) if op_vars else None)

would resolve the issue, however I am not sure if this is really the fix to the problem.

The problem is that the first implementation of WithItemBuilder that you linked makes use of features of the ast module that were introduced in python3.8. The second one should be compatible with 3.8 as well as older versions and was introduced because of a previous bug report that WithItemBuilder did not work in 3.6.