-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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: Different FormattedValue expressions have same col_offset information #81639
Comments
This express f"{x}{x}{y}" will produce ast tree: { These two variable 'x' has same col_offset '3', is it wrong? |
I'm working on overhauling how these are calculated. But it's complex, and is taking a while. |
I still see this problem with 3.10, which I thought might have fixed this. @lys.nikolaou: any ideas on this? |
Another alternative, in case we don't want to wait until then, would be for the handwritten f-string parser to have its own instances of a lineno and col_offset, so that they can be used when the FormattedValue nodes are created. This would probably also require some effort though, so I'm not sure we want to do it, before we really know if we're gonnna proceed with the "moving f-string parsing to PEG" project. |
I think waiting until we decide what to do with the parser makes sense. This problem has been around for a while, and while it's unfortunate I don't think it's worth heroic measures to fix. |
This was fixed in #27729, right? |
I'm not sure whether it's been completely fixed, definitely there's been improvements since the issue was opened here. We're working on moving f-string parsing into the PEG parser, which'll certainly help close this once and for all. |
The PR I linked includes fixing FormattedValues which contain identical-looking expressions, which I think is the problem being described here. The specific example is fixed for me in 3.9.7 and 3.10. Are there any remaining problems you know of relating to incorrect lineno/col_offset in AST nodes? |
It seems not fixed at least until 3.11 Python 3.11.1 (main, Feb 4 2023, 11:11:18) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin
>>> import ast
>>> expr = ast.parse('''\n\n\n\n\nf"Warning: '{fieldname}' should be a list, got type '{typename}'"''').body[0]
>>> expr.value.values
[<ast.Constant object at 0x1004e9c90>, <ast.FormattedValue object at 0x1004ebc70>, <ast.Constant object at 0x1004ea2c0>, <ast.FormattedValue object at 0x1004eb3a0>, <ast.Constant object at 0x1004eb190>]
>>> def l(n): return ((n.lineno, n.col_offset), (n.end_lineno, n.end_col_offset))
...
>>> for v in expr.value.values: print(l(v))
...
((6, 0), (6, 65))
((6, 0), (6, 65))
((6, 0), (6, 65))
((6, 0), (6, 65))
((6, 0), (6, 65)) |
This has been fixed in 3.12 after the PEP 701 implementation (#102855) was merged. Closing. |
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: