You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Decide how to handle comments with debug expressions in f-strings #112243
After PEP 701, having comments mixed with debug expressions is possible but the interactions has not been discussed. For instance, consider this code:
a = 1
f"{a = # my comment
}"
this produces:
'a = # my comment\n1'
which is surprising. What we should do here? I think we should probably not include the comment but we should agree on the behavior before a PR can be made
I expected the example to produce "SyntaxError: unterminated f-string literal (detected at line 1)", just as it would without the '{'. But given that '{' escapes the newline, I also expect it to escape the octothorpe, just as it does now. So I consider the current behavior correct.
@terryjreedy not sure I follow your reasoning, what do you mean when you say that "given that '{' escapes the newline"? There is nothing here being scaped no? Inside an f-string expression now any expression can be made as if it written between parentheses. The key of this issue is the debug expression here
I was using a different analogy for my expectation. 'a = #com\n3' is a SyntaxError. '''a= #com\n3''' is not, and the comment is keep. Your comment suggests this instead: (a * #com by itself is a Syntaxerror; (a * #com\n3 is not, because the parentheses cause the comment and newline to be ignored.
Even better, I think, are these: f'{a #com\n}' ignores the comment and its terminating newline and prints 1. The a = construct is unusual because 'a =' is quoted while 'a' is executed. What happens with text between '=' and '}'? f'{a=x}' raises SyntaxError: f-string: expecting '!', or ':', or '}'. These examples convince me that a comment after '=' should be recognized as such and ignored rather that quoted as additional text.
As for newlines: whitespace before and after = is kept, whether or not #whatever is present, and newlines can be escaped with \. Leave this alone.
If the expression component of an f-string requires comments, you're probably doing something horribly wrong. That being said, keeping the comment in the resulting string is likely to be the least surprising behavior. Either that or forbidding comments inside f-strings altogether.
Activity
pablogsal commentedon Nov 18, 2023
@isidentical @lysnikolaou
ericvsmith commentedon Nov 18, 2023
I think we should remove the comment and keep the newline.
terryjreedy commentedon Nov 18, 2023
I expected the example to produce "SyntaxError: unterminated f-string literal (detected at line 1)", just as it would without the '{'. But given that '{' escapes the newline, I also expect it to escape the octothorpe, just as it does now. So I consider the current behavior correct.
pablogsal commentedon Nov 18, 2023
@terryjreedy not sure I follow your reasoning, what do you mean when you say that "given that '{' escapes the newline"? There is nothing here being scaped no? Inside an f-string expression now any expression can be made as if it written between parentheses. The key of this issue is the debug expression here
terryjreedy commentedon Nov 19, 2023
I was using a different analogy for my expectation.
'a = #com\n3'is a SyntaxError.'''a= #com\n3'''is not, and the comment is keep. Your comment suggests this instead:(a * #comby itself is a Syntaxerror;(a * #com\n3is not, because the parentheses cause the comment and newline to be ignored.Even better, I think, are these:
f'{a #com\n}'ignores the comment and its terminating newline and prints1. Thea =construct is unusual because 'a =' is quoted while 'a' is executed. What happens with text between '=' and '}'?f'{a=x}'raisesSyntaxError: f-string: expecting '!', or ':', or '}'. These examples convince me that a comment after '=' should be recognized as such and ignored rather that quoted as additional text.As for newlines: whitespace before and after
=is kept, whether or not #whatever is present, and newlines can be escaped with\. Leave this alone.magistau commentedon Nov 19, 2023
If the expression component of an f-string requires comments, you're probably doing something horribly wrong. That being said, keeping the comment in the resulting string is likely to be the least surprising behavior. Either that or forbidding comments inside f-strings altogether.
lysnikolaou commentedon Nov 20, 2023
I also think that the comment should be ignored, while the newline should be kept.
pythongh-112243: Don't include comments in f-string debug expressions
fixup! pythongh-112243: Don't include comments in f-string debug expr…
gh-112243: Don't include comments in f-string debug expressions (#112284
[3.12] pythongh-112243: Don't include comments in f-string debug expr…
[3.12] gh-112243: Don't include comments in f-string debug expressions (
pythongh-112243: Don't include comments in f-string debug expressions (…
pythongh-112243: Don't include comments in f-string debug expressions (…