Skip to content

Commit 59d5c34

Browse files
committed
gh-102856: Allow comments inside multi-line f-string expresions
1 parent cdd491c commit 59d5c34

File tree

2 files changed

+4
-9
lines changed

2 files changed

+4
-9
lines changed

Lib/test/test_fstring.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -644,10 +644,11 @@ def test_comments(self):
644644
self.assertEqual(f'{"#"}', '#')
645645
self.assertEqual(f'{d["#"]}', 'hash')
646646

647-
self.assertAllRaise(SyntaxError, "f-string expression part cannot include '#'",
648-
["f'{1#}'", # error because the expression becomes "(1#)"
649-
"f'{3(#)}'",
647+
self.assertAllRaise(SyntaxError, "'{' was never closed",
648+
["f'{1#}'", # error because everything after '#' is a comment
650649
"f'{#}'",
650+
"f'one: {1#}'",
651+
"f'{1# one} {2 this is a comment still#}'",
651652
])
652653
self.assertAllRaise(SyntaxError, r"f-string: unmatched '\)'",
653654
["f'{)#}'", # When wrapped in parens, this becomes

Parser/tokenizer.c

-6
Original file line numberDiff line numberDiff line change
@@ -1793,12 +1793,6 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
17931793
/* Skip comment, unless it's a type comment */
17941794
if (c == '#') {
17951795

1796-
// We don't allow # inside a one-line f-string expr,
1797-
// but we do in multi-line f-string expr
1798-
if (INSIDE_FSTRING(tok) && (tok->lineno == 1)) {
1799-
return MAKE_TOKEN(syntaxerror(tok, "f-string expression part cannot include '#'"));
1800-
}
1801-
18021796
const char *prefix, *p, *type_start;
18031797
int current_starting_col_offset;
18041798

0 commit comments

Comments
 (0)