This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ast.Tuple's have an inconsistent "col_offset" value
Type: behavior Stage: needs patch
Components: Versions: Python 3.2, Python 3.3, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, bronikkk, georg.brandl, meador.inge
Priority: normal Keywords:

Created on 2012-01-09 16:33 by bronikkk, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
sample01.py bronikkk, 2012-01-09 16:33
Messages (4)
msg150950 - (view) Author: Igor Bronshteyn (bronikkk) Date: 2012-01-09 16:33
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).
msg150951 - (view) Author: Igor Bronshteyn (bronikkk) Date: 2012-01-09 16:36
I mean, that AST generated with standard "ast.parse" has nodes with confusing positions. Sorry for the first indistinct message.
msg151162 - (view) Author: Meador Inge (meador.inge) * (Python committer) Date: 2012-01-13 04:00
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'.
msg151190 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2012-01-13 19:47
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.
History
Date User Action Args
2022-04-11 14:57:25adminsetgithub: 57955
2012-01-13 19:47:03georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg151190

resolution: not a bug
2012-01-13 04:00:26meador.ingesetversions: + Python 3.2, Python 3.3, - Python 2.6
nosy: + meador.inge, benjamin.peterson

messages: + msg151162

stage: needs patch
2012-01-09 16:36:52bronikkksetmessages: + msg150951
2012-01-09 16:33:56bronikkkcreate