This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: caret location for syntax error pointing with f-strings
Type: Stage:
Components: Parser Versions: Python 3.11, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, lys.nikolaou, pablogsal, williamnavaraj
Priority: normal Keywords:

Created on 2022-01-06 01:25 by williamnavaraj, last changed 2022-04-11 14:59 by admin.

Messages (3)
msg409813 - (view) Author: William Navaraj (williamnavaraj) * Date: 2022-01-06 01:25
Currently for non-f-string syntax errors, 
the caret points to the correct location of the syntax errors

Example 1:

```
a=foo)+foo()+foo()

```
    a=foo)+foo()+foo()
         ^
SyntaxError: unmatched ')'


For f-string syntax errors, 
the caret points two locations after the f-string (almost correct location of the f-strings as a whole but will be more helpful as much as possible to point where exactly the syntax error is)

Example 2:

```
temp=f"blank ({foo(}" 

```
    temp=f"blank ({foo(}" 
                          ^
SyntaxError: f-string: closing parenthesis '}' does not match opening parenthesis '('


Example 3:

```
temp=f"blank ({foo)blank ({foo()}) blank foo()})"

```
    temp=f"blank ({foo)blank ({foo()}) blank foo()})"
                                                     ^
SyntaxError: f-string: unmatched ')'
msg409814 - (view) Author: William Navaraj (williamnavaraj) * Date: 2022-01-06 01:52
A potential solution or in that direction

https://github.com/williamnavaraj/cpython/tree/fix-issue-46275

Example 1:

```
temp=f"blank {foo)" 

```
    temp=f"blank {foo)" 
                     ^
SyntaxError: f-string: unmatched ')'


Example 2:

```
temp=f"blank ({foo)foo2" 

```
    temp=f"blank ({foo)foo2" 
                      ^
SyntaxError: f-string: unmatched ')'


Example 3:

```
temp=f"blank ({foo)blank ({foo()}) blank foo()})"

```
    temp=f"blank ({foo)blank ({foo()}) blank foo()})"
                     ^
SyntaxError: f-string: unmatched ')'
msg410186 - (view) Author: William Navaraj (williamnavaraj) * Date: 2022-01-10 04:51
The variation in the caret position was also due to the trailing spaces. This is now sorted in this solution with a factored out function to find out the correct offset.
https://github.com/python/cpython/compare/main...williamnavaraj:fix-issue-46275?expand=1

Tested against all of the following cases and it works great:

temp=f"blank ({foo()}"+f"blank ({foo(}"+f"blank ({foo()}"
temp=f"blank*{foo(*blank*foo()*blank*foo()}"+f"({foo(}"+f"blank ({foo(}"
a=foo)+foo()+foo()      
f"blank ({foo(blank ({foo()}) blank foo()})"   
temp=f"blank ({foo)foo2" 
temp=f"blank {foo)"
temp=f"blank {foo)"<Space double triple or more here>
temp=f"blank ({foo)blank ({foo()}) blank foo()})"  yetAnotherBlah
temp=f"blank ({foo)blank ({foo()}) blank foo()})"  yetAnotherBlahWithFurtherSpacesAfter
History
Date User Action Args
2022-04-11 14:59:54adminsetgithub: 90433
2022-01-10 04:51:36williamnavarajsetmessages: + msg410186
2022-01-06 09:48:42pablogsalsetnosy: + eric.smith
2022-01-06 01:52:28williamnavarajsetmessages: + msg409814
2022-01-06 01:25:48williamnavarajcreate