-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
f-string evaluation of conditional expressions with != operator seems to fail #129093
Comments
Tested on Windows 11 with 3.13 and same bug. |
Also: >>> f'{1==0=}'
'1==0=False'
>>> f'{1!=0=}'
'1True' |
Yeah! I think that any conditional expression containing != behaves in the same manner -- whatever the result of evaluating the expression is. |
Yes, I was just trying to point out that the |
The AST looks like this:
So it does evaluate A
|
This is a parsing issue: >>> ast.dump(ast.parse("f'{1 != 1=}'"))
"Module(body=[Expr(value=JoinedStr(values=[Constant(value='1 '), FormattedValue(value=Compare(left=Constant(value=1), ops=[NotEq()], comparators=[Constant(value=1)]), conversion=114)]))])"
>>> ast.dump(ast.parse("f'{1 == 1=}'"))
"Module(body=[Expr(value=JoinedStr(values=[Constant(value='1 == 1='), FormattedValue(value=Compare(left=Constant(value=1), ops=[Eq()], comparators=[Constant(value=1)]), conversion=114)]))])" |
This seems to fix it (with diff --git a/Parser/lexer/lexer.c b/Parser/lexer/lexer.c
index dbbb94a407c..3b433addbe1 100644
--- a/Parser/lexer/lexer.c
+++ b/Parser/lexer/lexer.c
@@ -212,7 +212,7 @@ _PyLexer_update_fstring_expr(struct tok_state *tok, char cur)
case '}':
case '!':
case ':':
- if (tok_mode->last_expr_end == -1) {
+ if (tok_mode->last_expr_end == -1 || cur == '}') {
tok_mode->last_expr_end = strlen(tok->start);
}
break; I'll try to add some tests and perhaps open a PR tomorrow |
Could this be related to #124363? |
Could be, @picnixz, but I do not think so. As @JelleZijlstra pointed out:
while #124363, as long as I understand, is related to raw strings and escape characters, which is not the case here. The problem here seems to be that the expression text is printed up to
Notice that:
Even |
…en expression contains `!` (pythonGH-129159) (cherry picked from commit 767cf70) Co-authored-by: Tomas R. <tomas.roun8@gmail.com>
… off when expression contains !
Thanks everyone for the investigation and thanks @tomasr8 for the analysis. Fantastic job! |
…hen expression contains ! (#129164)
Thanks to everybody. This was awesome. |
Bug report
Bug description:
Hi,
I really feel puzzled by this behaviour:
I expected that the output of
print(f'{True != True=}')
would beTrue != True=False
.According to docs, "[t]o display both the expression text and its value after evaluation, (useful in debugging), an equal sign '=' may be added after the expression". So
print(f'{True != True=}')
fails because the expression text is not fully displayed -- even "=" is omitted.Sorry if this is a duplicate -- I was not able to find any other report here and in Cython issue tracker.
Thank you.
CPython versions tested on:
3.12, 3.13
Operating systems tested on:
macOS
Linked PRs
!
#129159!
(GH-129159) #129163The text was updated successfully, but these errors were encountered: