GeezerVison is showing github.com — Decide how to handle comments with debug expressions in f-strings · Issue #112243 · python/cpython
Decide how to handle comments with debug expressions in f-strings #112243DescriptionAfter 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:
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
Linked PRs
Activitypablogsal commented on Nov 18, 2023 ericvsmith commented on Nov 18, 2023 I think we should remove the comment and keep the newline. Reacted by Eli and Oh seungmin terryjreedy commented on 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. Reacted by Ethan Furman and Igor Morozov pablogsal commented on 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 commented on 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 * #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. magistau commented on 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 commented on Nov 20, 2023 I also think that the comment should be ignored, while the newline should be kept.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|