Issue 13746: ast.Tuple's have an inconsistent "col_offset" value (original) (raw)
Consider the following snippet (the file is attached):
==== code starts ==== a = [1, 3.14, 'abc', u'XYZ'] b = (1, 3.14, 'abc', u'XYZ') c = {1 : 3.14, 'abc' : u'XYZ'} ===== code ends =====
The list has correct position: (1,4), the dict has correct position too: (3,4). But the position of tuple node (ast.Tuple) has wrong or inconsistent "col_offset" = 5: (2,5).
I can reproduce this in tip as well:
ast.dump(ast.parse('a = (1,2)'), include_attributes=True) "Module(body=[Assign(targets=[Name(id='a', ctx=Store(), lineno=1, col_offset=0)], value=Tuple(elts=[Num(n=1, lineno=1, col_offset=5), Num(n=2, lineno=1, col_offset=7)], ctx=Load(), lineno=1, col_offset=5), lineno=1, col_offset=0)])" ast.dump(ast.parse('a = (1 + 2)'), include_attributes=True) "Module(body=[Assign(targets=[Name(id='a', ctx=Store(), lineno=1, col_offset=0)], value=BinOp(left=Num(n=1, lineno=1, col_offset=5), op=Add(), right=Num(n=2, lineno=1, col_offset=9), lineno=1, col_offset=5), lineno=1, col_offset=0)])"
The actual column number is lost when calling into 'ast_for_testlist' from the 'LPAR' case of 'ast_for_atom'.
This is because the parentheses don't really belong to the tuple literal.
You could just as well write
b = 1, 3.14, 'abc', u'XYZ'
In other cases, the parentheses may be needed for grouping purposes (e.g. in function calls), but they still are only for grouping, just as in (a + b) * c.
For the empty tuple, where the parentheses actually are part of the literal, the col_offset is correct.