You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Expressions with format specifiers in f-strings give wrong code position in AST #79393
bpo-44885: Incorrect exception highlighting for fstring format
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='https://github.com/ericvsmith'closed_at=<Date2022-03-16.12:23:28.718>created_at=<Date2018-11-11.12:34:00.842>labels= ['interpreter-core', 'type-bug', '3.7']
title='Expressions with format specifiers in f-strings give wrong code position in AST'updated_at=<Date2022-03-16.12:23:28.718>user='https://github.com/numirias'
Activity
numirias commentedon Nov 11, 2018
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}' :
In both examples, the name "a" is at the same position in the source code, however the parsed ASTs yield two different offsets,
and
.
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.)
numirias commentedon Oct 4, 2019
There is another instance of incorrectly reported offsets in f-strings, in this case affecting multi-line strings.
Example:
(
f'aaa{foo}bbb'
f'ccc{bar}ddd'
f'eee{baz}fff'
)
Relevant part of the syntax tree:
JoinedStr(
lineno=2,
col_offset=4,
values=[
Str(lineno=2, col_offset=4, s='aaa'),
FormattedValue(
lineno=2,
col_offset=4,
value=Name(lineno=2, col_offset=10, id='foo', ctx=Load()),
conversion=-1,
format_spec=None,
),
Str(lineno=2, col_offset=4, s='bbbccc'),
FormattedValue(
lineno=2,
col_offset=4,
value=Name(lineno=2, col_offset=5, id='bar', ctx=Load()),
conversion=-1,
format_spec=None,
),
Str(lineno=2, col_offset=4, s='dddcccaaaaaaaaaaaaaa'),
FormattedValue(
lineno=2,
col_offset=4,
value=Name(lineno=2, col_offset=5, id='baz', ctx=Load()),
conversion=-1,
format_spec=None,
),
Str(lineno=2, col_offset=4, s='ddd'),
],
),
The reported code position for "foo" is correct (2,10), but not for "bar" (2,5) and "baz" (2,5).
Could you give an estimate as to whether this issue will be looked at any time soon?
asmeurer commentedon Aug 31, 2020
The same thing occurs with specifiers like {a!r}.
DanielNoord commentedon Mar 16, 2022
This was fixed by #27729.
On 3.10:
The other issue also no longer reproduces.