-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
Error message differs when an expression is in an fstring #84448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
There are cases, where the error message differs, when an expression is being parsed inside an fstring. For example: >>> f'{x+}'
File "<fstring>", line 1
(x+)
^
SyntaxError: unexpected EOF while parsing
>>> (x+)
File "<stdin>", line 1
(x+)
^
SyntaxError: invalid syntax Or with lambda definitions: >>> f'{lambda x:x}'
File "<fstring>", line 1
(lambda x)
^
SyntaxError: unexpected EOF while parsing
>>> (lambda x)
File "<stdin>", line 1
(lambda x)
^
SyntaxError: invalid syntax |
It seems that this is actually a bit bigger than this and it is not specific to f-strings. The error message always changes to >>> exec('x+')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
x+
^
SyntaxError: invalid syntax
>>> eval('x+')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
x+
^
SyntaxError: unexpected EOF while parsing That's because the tokenizer adds an implicit newline to the input string, before tokenizing it, when the input comes from an exec call. (see Line 648 in 14d5331
And that's not limited to a character missing, as suggested by the error message. Even when the last character itself generates a SyntaxError, the error message remains "unexpcted EOF while parsing": >>> x+@
File "<stdin>", line 1
x+@
^
SyntaxError: invalid syntax
>>> eval('x+@')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
x+@
^
SyntaxError: unexpected EOF while parsing Thus, a very simple fix to the specific fstring problem of this issue would be to add a newline to the string that gets parsed to the parser in fstring_compile_expr in ast.c, but I guess it'd be better to fix the tokenizer itself, if this is considered a bug. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: