Skip to content

Commit 7b44ade

Browse files
bpo-47129: Add more informative messages to f-string syntax errors (32127)
* Add more informative messages to f-string syntax errors * 📜🤖 Added by blurb_it. * Fix whitespaces * Change error message * Remove the 'else' statement (as sugested in review) Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
1 parent 15ba816 commit 7b44ade

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

Lib/test/test_fstring.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -628,33 +628,51 @@ def test_missing_expression(self):
628628
["f'{}'",
629629
"f'{ }'"
630630
"f' {} '",
631-
"f'{!r}'",
632-
"f'{ !r}'",
633631
"f'{10:{ }}'",
634632
"f' { } '",
635633

636634
# The Python parser ignores also the following
637635
# whitespace characters in additional to a space.
638636
"f'''{\t\f\r\n}'''",
637+
])
638+
639+
# Different error messeges are raised when a specfier ('!', ':' or '=') is used after an empty expression
640+
self.assertAllRaise(SyntaxError, "f-string: expression required before '!'",
641+
["f'{!r}'",
642+
"f'{ !r}'",
643+
"f'{!}'",
644+
"f'''{\t\f\r\n!a}'''",
645+
646+
# Catch empty expression before the
647+
# missing closing brace.
648+
"f'{!'",
649+
"f'{!s:'",
639650

640-
# Catch the empty expression before the
651+
# Catch empty expression before the
641652
# invalid conversion.
642653
"f'{!x}'",
643654
"f'{ !xr}'",
644655
"f'{!x:}'",
645656
"f'{!x:a}'",
646657
"f'{ !xr:}'",
647658
"f'{ !xr:a}'",
659+
])
648660

649-
"f'{!}'",
650-
"f'{:}'",
651-
652-
# We find the empty expression before the
653-
# missing closing brace.
654-
"f'{!'",
655-
"f'{!s:'",
661+
self.assertAllRaise(SyntaxError, "f-string: expression required before ':'",
662+
["f'{:}'",
663+
"f'{ :!}'",
664+
"f'{:2}'",
665+
"f'''{\t\f\r\n:a}'''",
656666
"f'{:'",
657-
"f'{:x'",
667+
])
668+
669+
self.assertAllRaise(SyntaxError, "f-string: expression required before '='",
670+
["f'{=}'",
671+
"f'{ =}'",
672+
"f'{ =:}'",
673+
"f'{ =!}'",
674+
"f'''{\t\f\r\n=}'''",
675+
"f'{='",
658676
])
659677

660678
# Different error message is raised for other whitespace characters.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve error messages in f-string syntax errors concerning empty expressions.

Parser/string_parser.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,12 @@ fstring_compile_expr(Parser *p, const char *expr_start, const char *expr_end,
357357
break;
358358
}
359359
}
360+
360361
if (s == expr_end) {
362+
if (*expr_end == '!' || *expr_end == ':' || *expr_end == '=') {
363+
RAISE_SYNTAX_ERROR("f-string: expression required before '%c'", *expr_end);
364+
return NULL;
365+
}
361366
RAISE_SYNTAX_ERROR("f-string: empty expression not allowed");
362367
return NULL;
363368
}

0 commit comments

Comments
 (0)