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
Comments
The >>> 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. |
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 |
For comparison, a tuple without parentheses: >>> ast.parse("1,2,3").body[0].value.col_offset
0 |
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 |
Sorry, I mean the parenthesis is *not* required. |
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. |
Oh, and the empty tuple is a specific syntactic construct that really is the empty parenthesis, so that's consistent with the language definition. |
Fair enough, thanks for clarification. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: