◐ Shell
clean mode source ↗

Issue 44885: Incorrect exception highlighting for fstring format

Given this code:

print(f"Here is that pesky {xxx/2:.3f} again")

The traceback prints:

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
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:

print(f"Here is that {xxx} pesky {xxx} again")

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

print(f"Here is that {xxx} pesky {xxxx} again")

(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),
...