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: Improve errors messages in f-string syntax errors
Type: behavior Stage: resolved
Components: Parser Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: eric.smith, lys.nikolaou, macgors, pablogsal
Priority: normal Keywords: patch

Created on 2022-03-26 15:08 by macgors, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 32127 merged macgors, 2022-03-26 15:09
Messages (9)
msg416069 - (view) Author: Maciej Górski (macgors) * Date: 2022-03-26 15:08
When an empty expression is provided in curly brackets inside an f-string we get Syntax Error: "f-string: empty expression not allowed".


This is correct, however error with the same message is raised in the following cases: f"{!foo}", f"{!}", f"{:bar}", f"{=}", etc.
For example, should someone make an error and try to negate an expression with '!' instead of 'not' (i.e. f"{!True}"), they would get a confusing error, saying that the expression is empty, when it isn't. 

I propose we raise an error with the message: "f-string: optional specifier '%c' must follow a non-empty expression.
msg416075 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-03-26 16:57
I'm not convinced this is an improvement. I don't think someone trying '!' in an f-string expression happens often enough to worry about that specific error message.

And a generic "optional specifier" isn't great. If we're going to do this, it should reflect the actual thing that's present: a conversion specifier, a format specifier, or debug specifier. And "optional" doesn't add anything, since it's actually present in this string.

I'll give it some more thought. I can't come up with an error message I actually like. The focus still needs to be on the empty expression.
msg416077 - (view) Author: Maciej Górski (macgors) * Date: 2022-03-26 18:04
Understandable. In this case I can maybe propose something along the lines of "f-string: empty expression not allowed (Conversion specifier '!' can only follow non-empty expressions)", and so on for each of the specifiers.

This way the original message is still present and we just add some more information. I'm open to any of your ideas.
msg416095 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-03-26 22:26
From a suggestion by @Jelle on the python discord server: how about just "f-string: expression required before '!'"?
msg416127 - (view) Author: Lysandros Nikolaou (lys.nikolaou) * (Python committer) Date: 2022-03-27 15:41
I like the newest suggestion by @Jelle better as well.
msg416128 - (view) Author: Maciej Górski (macgors) * Date: 2022-03-27 15:44
I agree as well. I will change it to the new version :) Thanks for your input in this.
msg416215 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-03-28 21:08
New changeset 7b44ade018cfe6f54002a3cee43e8aa415d4d635 by Maciej Górski in branch 'main':
bpo-47129: Add more informative messages to f-string syntax errors (32127)
https://github.com/python/cpython/commit/7b44ade018cfe6f54002a3cee43e8aa415d4d635
msg416216 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-03-28 21:09
Thanks, @macgors!
msg416217 - (view) Author: Maciej Górski (macgors) * Date: 2022-03-28 21:12
Thanks for helping me with my very first python contribution! @eric.smith @lys.nikolaou
History
Date User Action Args
2022-04-11 14:59:57adminsetgithub: 91285
2022-03-28 21:12:40macgorssetmessages: + msg416217
2022-03-28 21:09:44eric.smithsetstatus: open -> closed
type: behavior
messages: + msg416216

resolution: fixed
stage: patch review -> resolved
2022-03-28 21:08:44eric.smithsetmessages: + msg416215
2022-03-27 15:44:46macgorssetmessages: + msg416128
2022-03-27 15:41:54lys.nikolaousetmessages: + msg416127
2022-03-26 22:26:19eric.smithsetmessages: + msg416095
2022-03-26 18:04:56macgorssetmessages: + msg416077
2022-03-26 16:57:40eric.smithsetassignee: eric.smith
messages: + msg416075
2022-03-26 15:12:17pablogsalsetnosy: + eric.smith
2022-03-26 15:09:44macgorssetkeywords: + patch
stage: patch review
pull_requests: + pull_request30206
2022-03-26 15:08:27macgorscreate