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: Signify that a SyntaxError comes from an fstring in the error message
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, gvanrossum, lys.nikolaou, miss-islington, pablogsal, vstinner
Priority: normal Keywords: patch

Created on 2020-06-22 23:53 by lys.nikolaou, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 21084 merged lys.nikolaou, 2020-06-23 15:01
PR 21188 merged miss-islington, 2020-06-27 19:25
PR 21212 merged lys.nikolaou, 2020-06-29 11:13
Messages (11)
msg372134 - (view) Author: Lysandros Nikolaou (lys.nikolaou) * (Python committer) Date: 2020-06-22 23:53
It's relatively easy to identify if a SyntaxError occurs when parsing an fstring expression or not. The idea is to slightly change the error message to start with "f-string: " when it does (in the same way in which SyntaxError messages are produced by the hand-written fstring parser). I have a working PR that produces the following:

$ cat a.py
f'{a $ b}'
$ ./python.exe a.py
  File "/Users/lysnikolaou/Repositories/cpython/a.py", line 1
    (a $ b)
       ^
SyntaxError: f-string: invalid syntax

Thoughts?
msg372136 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-06-23 00:49
I think it's a good idea.

My only reservation would be: if the parsing of f-strings is moved into the parser, would it be possible to maintain the error new messages?
msg372150 - (view) Author: Lysandros Nikolaou (lys.nikolaou) * (Python committer) Date: 2020-06-23 09:46
> My only reservation would be: if the parsing of f-strings is moved into the parser, would it be possible to maintain the error new messages?

Probably without a single change.
msg372422 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-06-26 11:24
New changeset 2e0a920e9eb540654c0bb2298143b00637dc5961 by Lysandros Nikolaou in branch 'master':
bpo-41084: Adjust message when an f-string expression causes a SyntaxError (GH-21084)
https://github.com/python/cpython/commit/2e0a920e9eb540654c0bb2298143b00637dc5961
msg372471 - (view) Author: Lysandros Nikolaou (lys.nikolaou) * (Python committer) Date: 2020-06-27 18:11
Should we maybe backport this to 3.9 as well?
msg372472 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-06-27 18:31
I would say yes unless someone sees an obvious impediment
msg372477 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-06-27 19:18
AFAIK, we don't guarantee the stability of error messages, so I think backporting is fine. It's all the better that this is just prefixing something to an existing error message.
msg372478 - (view) Author: miss-islington (miss-islington) Date: 2020-06-27 19:43
New changeset cb0dc52d37a5cc561ad39bc034afe7db9c461768 by Miss Islington (bot) in branch '3.9':
bpo-41084: Adjust message when an f-string expression causes a SyntaxError (GH-21084)
https://github.com/python/cpython/commit/cb0dc52d37a5cc561ad39bc034afe7db9c461768
msg372562 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-06-29 10:56
The change broke the old parser in Python 3.9, I reopen the issue:

AMD64 Arch Linux VintageParser 3.9:
https://buildbot.python.org/all/#/builders/765/builds/129

FAIL: test_format_specifier_expressions (test.test_fstring.TestCase) (str="f'{4:{/5}}'")
FAIL: test_invalid_syntax_error_message (test.test_fstring.TestCase)
FAIL: test_lambda (test.test_fstring.TestCase) (str="f'{lambda x:x}'")
FAIL: test_parens_in_expressions (test.test_fstring.TestCase) (str="f'{,}'")
FAIL: test_parens_in_expressions (test.test_fstring.TestCase) (str="f'{,}'")
msg372570 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-06-29 12:56
New changeset 990efe0fff749773887a50cb1ecb2c5d09263cf8 by Lysandros Nikolaou in branch '3.9':
[3.9] bpo-41084: Fix test_fstring failure when using the old parser (GH-21212)
https://github.com/python/cpython/commit/990efe0fff749773887a50cb1ecb2c5d09263cf8
msg372572 - (view) Author: Lysandros Nikolaou (lys.nikolaou) * (Python committer) Date: 2020-06-29 13:03
Thanks, Victor!

The fix is merged, so I'm closing this again.
History
Date User Action Args
2022-04-11 14:59:32adminsetgithub: 85256
2020-06-29 13:03:43lys.nikolaousetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-06-29 13:03:31lys.nikolaousetmessages: + msg372572
2020-06-29 12:56:01pablogsalsetmessages: + msg372570
2020-06-29 11:13:46lys.nikolaousetstage: resolved -> patch review
pull_requests: + pull_request20366
2020-06-29 10:56:08vstinnersetstatus: closed -> open

nosy: + vstinner
messages: + msg372562

resolution: fixed -> (no value)
2020-06-27 19:43:55miss-islingtonsetmessages: + msg372478
2020-06-27 19:25:10miss-islingtonsetnosy: + miss-islington

pull_requests: + pull_request20342
2020-06-27 19:18:02eric.smithsetmessages: + msg372477
2020-06-27 18:31:42pablogsalsetmessages: + msg372472
2020-06-27 18:11:37lys.nikolaousetmessages: + msg372471
2020-06-26 20:42:31lys.nikolaousetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-06-26 11:24:31pablogsalsetmessages: + msg372422
2020-06-23 15:01:56lys.nikolaousetkeywords: + patch
stage: patch review
pull_requests: + pull_request20250
2020-06-23 09:46:00lys.nikolaousetmessages: + msg372150
2020-06-23 00:49:06eric.smithsetmessages: + msg372136
2020-06-22 23:53:42lys.nikolaoucreate