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: Insufficient error message with incorrect formated string literal
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.9, Python 3.7, Python 3.6
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: PeterL777, chris.259263, cito, eric.smith, philiprowlands
Priority: normal Keywords:

Created on 2017-08-08 08:55 by chris.259263, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 1800 lukasz.langa, 2017-09-06 18:18
Messages (9)
msg299901 - (view) Author: (chris.259263) Date: 2017-08-08 08:55
Trying to run a script with a bug in a formated string literal (in the example the ":" is missing) throws an "invalid syntax" error at the first expression of the script.

Example:

import math
a = "a"
b = f"{a>2s}"   # forgotten ":", should be f"{a:>2s}"

Running this script throws an "invalid syntax" error in the first line "import math".

In a large program this behavior makes it nearly impossible to find the real bug in the program as the error message does not give any hint about the line or the type of the error in the program.

Using Python 3.6.2, IDLE 3.6.2 on Mac macOS Sierra (10.12.1)
msg299903 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2017-08-08 11:13
On 3.6.1 on Windows, I get:

% python3 bpo-31140.py
  File "<fstring>", line 1
    (a>2s)
        ^
SyntaxError: invalid syntax

Which is far from ideal, but at least points to (a portion of) the correct text.

I won't have access to a copy of 3.6.2 or 3.7 until later today. Can you show the exact output from 3.6.2?
msg299906 - (view) Author: (chris.259263) Date: 2017-08-08 11:43
Hi Eric,

running the script from the terminal on my system (macOS, Python 3.6.2) gives the same ErrorMessage as you stated.

The problem is the output "line 1".

Running the script from IDLE (3.6.2), the error points to the first line. And in IDLE you do not get the useful hint "(a>2s)" but just "SyntaxError: invalid syntax".
msg299907 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2017-08-08 11:46
Thanks. There was some work on this recently: I'll try and check 3.7 later today.
msg309773 - (view) Author: Christoph Zwerschke (cito) * Date: 2018-01-10 16:33
I can confirm that the problem still exists in Python 3.6.4 and 3.7.0a4.

Here is another way to demonstrate it:

Create the following file test.py:

    # test
    hello = f"{world)}"

Note that there is a syntax error in the f-string in line 2 which has a closing parentheses, but no opening one.

Import this from Python 3.6.4 or 3.7.0a4:

>>> import test
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<fstring>", line 1
    (world))
           ^
SyntaxError: unexpected EOF while parsing

The problem here is that the error message does not contain the name of the erroneous file (test.py), points to a wrong line (line 1 instead of line 2), and also shows parentheses instead of braces around the word "world", which are not there in the original code. This can make it hard to locate such errors.

Note that when there are other kinds of errors in the f-string, or other kinds of "unexpected EOF" in the imported file, the errorenous file is usually reported correctly in the error message. Only certain kinds of syntax errors in f-strings seem to be problematic.
msg358410 - (view) Author: Philip Rowlands (philiprowlands) Date: 2019-12-15 01:50
Status as of 3.9.0a1:

======
test.py above appears fixed, i.e. reasonable error message.

$ ./python test.py
  File "/home/bob/pybug/Python-3.9.0a1/test.py", line 2
    hello = f"{world)}"
            ^
SyntaxError: f-string: unmatched ')'


======
bpo-31140.py is not as bad a initially reported, but still gives the wrong line number.

$ ./python bpo-31140.py
  File "<fstring>", line 1
    (a>2s)
        ^
SyntaxError: invalid syntax


=======
And my own example which led me to this bug. The syntax error is on line 3, not line 1.

$ cat fruit.py
pass
pass
s = f"{My favourite fruit is {apple}}"

$ ./python -V
Python 3.9.0a1

$ ./python fruit.py
  File "<fstring>", line 1
    (My favourite fruit is {apple})
        ^
SyntaxError: invalid syntax
msg416234 - (view) Author: Peter Lovett (PeterL777) Date: 2022-03-29 00:06
I'm not getting the problem on 3.9.7 on Windows. 
Did get it on 3.7 (3.7.11?) on a different Windows machine last week.
Not getting the problem on 3.10.4

The wrong line number is a problem for IDLE's syntax highlighter, that marks the first line as a Syntax Error, even if that line is a comment. 

This is an issue for ArcGIS Pro users, which is (I think) currently shipping with Python 3.7.11, so those users may come across it. Best solution would be for Esri to update their Python version to something newer.
https://support.esri.com/en/technical-article/000013224
msg416236 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-03-29 00:11
Yes, they should update Python. A lot of work went in to fixing these issues, and won't be backported.
msg416237 - (view) Author: Peter Lovett (PeterL777) Date: 2022-03-29 00:56
Thanks Eric.
History
Date User Action Args
2022-04-11 14:58:49adminsetgithub: 75323
2022-03-29 00:56:04PeterL777setmessages: + msg416237
2022-03-29 00:11:29eric.smithsetstatus: open -> closed
resolution: out of date
messages: + msg416236

stage: needs patch -> resolved
2022-03-29 00:06:37PeterL777setmessages: + msg416234
2022-03-28 23:54:36PeterL777setnosy: + PeterL777
2019-12-15 01:50:50philiprowlandssetnosy: + philiprowlands

messages: + msg358410
versions: + Python 3.9
2018-01-10 16:33:43citosetnosy: + cito
messages: + msg309773
2017-09-06 18:18:00lukasz.langasetpull_requests: + pull_request3396
2017-09-04 18:18:28eric.smithsetversions: + Python 3.7
2017-09-04 18:18:18eric.smithsetstage: needs patch
2017-08-08 11:46:25eric.smithsetmessages: + msg299907
2017-08-08 11:43:46chris.259263setmessages: + msg299906
2017-08-08 11:13:28eric.smithsetnosy: + eric.smith
messages: + msg299903
components: + Interpreter Core
2017-08-08 08:55:58chris.259263create