Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ast.Tuple has wrong col_offset #77701

Closed
isaiah mannequin opened this issue May 15, 2018 · 8 comments
Closed

ast.Tuple has wrong col_offset #77701

isaiah mannequin opened this issue May 15, 2018 · 8 comments
Labels
3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@isaiah
Copy link
Mannequin

isaiah mannequin commented May 15, 2018

BPO 33520
Nosy @bitdancer, @ambv, @isaiah

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2018-05-15.20:08:58.752>
created_at = <Date 2018-05-15.15:16:22.321>
labels = ['3.7', 'invalid', 'type-bug', 'library']
title = 'ast.Tuple has wrong col_offset'
updated_at = <Date 2018-05-16.08:26:54.299>
user = 'https://github.com/isaiah'

bugs.python.org fields:

activity = <Date 2018-05-16.08:26:54.299>
actor = 'isaiah'
assignee = 'none'
closed = True
closed_date = <Date 2018-05-15.20:08:58.752>
closer = 'r.david.murray'
components = ['Library (Lib)']
creation = <Date 2018-05-15.15:16:22.321>
creator = 'isaiah'
dependencies = []
files = []
hgrepos = []
issue_num = 33520
keywords = []
message_count = 8.0
messages = ['316665', '316684', '316685', '316708', '316710', '316713', '316715', '316785']
nosy_count = 3.0
nosy_names = ['r.david.murray', 'lukasz.langa', 'isaiah']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue33520'
versions = ['Python 3.7']

@isaiah
Copy link
Mannequin Author

isaiah mannequin commented May 15, 2018

The col_offset of the ast.Tuple node is set to the column offset of the first element, shown in code:

>>> a = "{1,2,3}"
>>> b = ast.parse(a).body[0]                                                                                                                                                                  
>>> b.value.col_offset
0
>>> a = "[1,2,3]"                                                                                                                                                                             
>>> b = ast.parse(a).body[0]                                                                                                                                                                  
>>> b.value.col_offset                                                                                                                                                                        
0                                                                                                                                                                                             
>>> a = "(1,2,3)"                                                                                                                                                                             
>>> ast.parse(a).body[0].value.col_offset                                                                                                                                                     
1 
>>> a = "()"                                                                                                                                                                                  
>>> ast.parse(a).body[0].value.col_offset                                                                                                                                                     
0  

It's correct for dict, set, list, even empty tuple, Though this is not a serious bug, for other python implementations that uses the tests as language spec, this is annoying.

@isaiah isaiah mannequin added 3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels May 15, 2018
@ambv
Copy link
Contributor

ambv commented May 15, 2018

This is because technically parentheses aren't part of the tuple. They are just organizational and unnecessary for the tuple to be recognized by the parser.

Those two are equivalent:

  >>> ast.parse("(1,2,3)").body[0].value.col_offset
  1
  >>> ast.parse("(1)").body[0].value.col_offset
  1

You can see similar behavior within generator expressions in contexts where the parentheses are not semantically required:

  >>> ast.parse("c(i for i in range(10))").body[0].value.args[0].col_offset
  2
  >>> ast.parse("c((i for i in range(10)))").body[0].value.args[0].col_offset
  3

@ambv
Copy link
Contributor

ambv commented May 15, 2018

For comparison, a tuple without parentheses:

  >>> ast.parse("1,2,3").body[0].value.col_offset
  0

@isaiah
Copy link
Mannequin Author

isaiah mannequin commented May 15, 2018

Thanks for the reply, that's quite reasonable, especially take the generator expression case into consideration. However I found this is not consistent with empty tuple:

>>> a = "()"                                                                                                                                                                                  
>>> ast.parse(a).body[0].value.col_offset                                                                                                                                                     
0

It's true that the parenthesis is required to construct a tuple, but if the parenthesis is served as the starting point of the tuple, then the col_offset should be the opening parenthesis. i.e. in the following example, both should start from col 2:

  >>> ast.parse("c(i for i in range(10))").body[0].value.args[0].col_offset
  2
  >>> ast.parse("c((i for i in range(10)))").body[0].value.args[0].col_offset
  3

@isaiah
Copy link
Mannequin Author

isaiah mannequin commented May 15, 2018

It's true that the parenthesis is required to construct a tuple

Sorry, I mean the parenthesis is *not* required.

@bitdancer
Copy link
Member

No, the parenthesis are never part of the tuple itself, even if you can't write syntactically correct code without them. They just syntactically group the expression list to isolate it from the surrounding context. It's the same principle as having an expression like (123). That's an integer that happens to be surrounded by parenthesis. The integer itself still starts at column 1.

@bitdancer
Copy link
Member

Oh, and the empty tuple is a specific syntactic construct that really is the empty parenthesis, so that's consistent with the language definition.

@isaiah
Copy link
Mannequin Author

isaiah mannequin commented May 16, 2018

Fair enough, thanks for clarification.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants