Message329672
ast.parse() will give a wrong code position for an expression inside an f-string when the expression is using a format specifier.
Compare the trees of f'{a}' and f'{a:b}' :
>>> ast.parse("f'{a}'")
Module(
body=[
Expr(
lineno=1,
col_offset=0,
value=JoinedStr(
lineno=1,
col_offset=0,
values=[
FormattedValue(
lineno=1,
col_offset=0,
value=Name(lineno=1, col_offset=3, id='a', ctx=Load()),
conversion=-1,
format_spec=None,
),
],
),
),
],
)
>>> ast.parse("f'{a:b}'")
Module(
body=[
Expr(
lineno=1,
col_offset=0,
value=JoinedStr( col_offset=0,
lineno=1,
col_offset=0,
values=[
FormattedValue(
lineno=1,
col_offset=0,
value=Name(lineno=1, col_offset=1, id='a', ctx=Load()),
conversion=-1,
format_spec=JoinedStr(
lineno=1,
col_offset=0,
values=[Str(lineno=1, col_offset=0, s='b')],
),
),
],
),
),
],
)
In both examples, the name "a" is at the same position in the source code, however the parsed ASTs yield two different offsets,
Name(lineno=1, col_offset=3, id='a', ctx=Load())
and
Name(lineno=1, col_offset=1, id='a', ctx=Load())
.
Expected behavior: col_offset=3 for name "a" in both f-strings.
(In my specific use case, this breaks a semantic highlighting plugin for vim.) |
|
Date |
User |
Action |
Args |
2018-11-11 12:34:00 | arminius | set | recipients:
+ arminius |
2018-11-11 12:34:00 | arminius | set | messageid: <1541939640.9.0.788709270274.issue35212@psf.upfronthosting.co.za> |
2018-11-11 12:34:00 | arminius | link | issue35212 messages |
2018-11-11 12:34:00 | arminius | create | |
|