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
Incorrect exception highlighting for fstring format #89048
Traceback (most recent call last):
File "/home/pablogsal/github/python/main/lel.py", line 1, in <module>print(f"Here is that pesky {xxx/2:.3f} again")
^^^NameError: name 'xxx' is not defined
Removing the formatting part ":.3f" makes it work as expected
The problem is that the strstr call will fail because the string containing the expression doesn't have the formatting part in it, so the offsets never get corrected.
Actually, this has even more problems. Because we are using strstr to find the start of the expression in the parent string, if the expression is repeated the offsets are incorrectly generated:
New changeset 8e832fb by Pablo Galindo Salgado in branch 'main': bpo-44885: Correct the ast locations of f-strings with format specs and repeated expressions (GH-27729) 8e832fb
New changeset c28c2e1 by Pablo Galindo Salgado in branch '3.10':
[3.10] bpo-44885: Correct the ast locations of f-strings with format specs and repeated expressions (GH-27729) (GH-27743) c28c2e1
New changeset 4b86c9c by Pablo Galindo Salgado in branch '3.9':
[3.9] bpo-44885: Correct the ast locations of f-strings with format specs and repeated expressions (GH-27729) (GH-27744) 4b86c9c
Activity
pablogsal commentedon Aug 11, 2021
Given this code:
The traceback prints:
Removing the formatting part ":.3f" makes it work as expected
gvanrossum commentedon Aug 11, 2021
(I originally reported this.)
pablogsal commentedon Aug 11, 2021
The problem lies here:
cpython/Parser/string_parser.c
Lines 374 to 389 in f66d00f
The problem is that the strstr call will fail because the string containing the expression doesn't have the formatting part in it, so the offsets never get corrected.
pablogsal commentedon Aug 11, 2021
Actually, this has even more problems. Because we are using strstr to find the start of the expression in the parent string, if the expression is repeated the offsets are incorrectly generated:
For example:
This produces:
...
FormattedValue(
value=Name(
id='xxx',
ctx=Load(),
lineno=1,
col_offset=22,
end_lineno=1,
end_col_offset=25),
...
FormattedValue(
value=Name(
id='xxxx',
ctx=Load(),
lineno=1,
col_offset=22,
end_lineno=1,
end_col_offset=25),
...
while
(different variables) produces:
...
FormattedValue(
value=Name(
id='xxx',
ctx=Load(),
lineno=1,
col_offset=22,
end_lineno=1,
end_col_offset=25),
...
FormattedValue(
value=Name(
id='xxxx',
ctx=Load(),
lineno=1,
col_offset=34,
end_lineno=1,
end_col_offset=38),
...
pablogsal commentedon Aug 11, 2021
Ha, we have test that knows is broken:
cpython/Lib/test/test_fstring.py
Line 217 in f66d00f
pablogsal commentedon Aug 12, 2021
New changeset 8e832fb by Pablo Galindo Salgado in branch 'main':
bpo-44885: Correct the ast locations of f-strings with format specs and repeated expressions (GH-27729)
8e832fb
pablogsal commentedon Aug 12, 2021
New changeset c28c2e1 by Pablo Galindo Salgado in branch '3.10':
[3.10] bpo-44885: Correct the ast locations of f-strings with format specs and repeated expressions (GH-27729) (GH-27743)
c28c2e1
pablogsal commentedon Aug 12, 2021
New changeset 4b86c9c by Pablo Galindo Salgado in branch '3.9':
[3.9] bpo-44885: Correct the ast locations of f-strings with format specs and repeated expressions (GH-27729) (GH-27744)
4b86c9c