Skip to content

Commit 143524b

Browse files
committed
specialize error message for lambda
1 parent 11cd51b commit 143524b

File tree

4 files changed

+52
-17
lines changed

4 files changed

+52
-17
lines changed

Grammar/python.gram

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,10 +1358,11 @@ invalid_kvpair:
13581358
invalid_starred_expression:
13591359
| a='*' expression '=' b=expression { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "cannot assign to iterable argument unpacking") }
13601360
invalid_replacement_field:
1361-
| '{' a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: expression required before '='") }
1362-
| '{' a='!' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: expression required before '!'") }
1363-
| '{' a=':' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: expression required before ':'") }
1364-
| '{' a='}' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: expression required before '}'") }
1361+
| '{' a='=' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: valid expression required before '='") }
1362+
| '{' a='!' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: valid expression required before '!'") }
1363+
| '{' a=':' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: valid expression required before ':'") }
1364+
| '{' a='}' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: valid expression required before '}'") }
1365+
| '{' a='lambda' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: lambda expression are not allowed without parentheses") }
13651366
| '{' !(yield_expr | star_expressions) { RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f-string: expecting a valid expression after '{'")}
13661367
| '{' (yield_expr | star_expressions) !('=' | '!' | ':' | '}') {
13671368
PyErr_Occurred() ? NULL : RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN("f-string: expecting '=', or '!', or ':', or '}'") }

Lib/test/test_cmd_line_script.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ def test_syntaxerror_multi_line_fstring(self):
638638
[
639639
b' foo = f"""{}',
640640
b' ^',
641-
b'SyntaxError: f-string: expression required before \'}\'',
641+
b'SyntaxError: f-string: valid expression required before \'}\'',
642642
],
643643
)
644644

Lib/test/test_fstring.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,8 @@ def __format__(self, spec):
722722
self.assertEqual(f'{x} {x}', '1 2')
723723

724724
def test_missing_expression(self):
725-
self.assertAllRaise(SyntaxError, "f-string: expression required before '}'",
725+
self.assertAllRaise(SyntaxError,
726+
"f-string: valid expression required before '}'",
726727
["f'{}'",
727728
"f'{ }'"
728729
"f' {} '",
@@ -734,7 +735,8 @@ def test_missing_expression(self):
734735
"f'''{\t\f\r\n}'''",
735736
])
736737

737-
self.assertAllRaise(SyntaxError, "f-string: expression required before '!'",
738+
self.assertAllRaise(SyntaxError,
739+
"f-string: valid expression required before '!'",
738740
["f'{!r}'",
739741
"f'{ !r}'",
740742
"f'{!}'",
@@ -755,15 +757,17 @@ def test_missing_expression(self):
755757
"f'{ !xr:a}'",
756758
])
757759

758-
self.assertAllRaise(SyntaxError, "f-string: expression required before ':'",
760+
self.assertAllRaise(SyntaxError,
761+
"f-string: valid expression required before ':'",
759762
["f'{:}'",
760763
"f'{ :!}'",
761764
"f'{:2}'",
762765
"f'''{\t\f\r\n:a}'''",
763766
"f'{:'",
764767
])
765768

766-
self.assertAllRaise(SyntaxError, "f-string: expression required before '='",
769+
self.assertAllRaise(SyntaxError,
770+
"f-string: valid expression required before '='",
767771
["f'{=}'",
768772
"f'{ =}'",
769773
"f'{ =:}'",
@@ -879,7 +883,8 @@ def test_backslashes_in_expression_part(self):
879883
self.assertEqual(f'{"\N{LEFT CURLY BRACKET}"}', '{')
880884
self.assertEqual(rf'{"\N{LEFT CURLY BRACKET}"}', '{')
881885

882-
self.assertAllRaise(SyntaxError, "f-string: expression required before '}'",
886+
self.assertAllRaise(SyntaxError,
887+
"f-string: valid expression required before '}'",
883888
["f'{\n}'",
884889
])
885890

@@ -926,7 +931,8 @@ def test_lambda(self):
926931
# lambda doesn't work without parens, because the colon
927932
# makes the parser think it's a format_spec
928933
self.assertAllRaise(SyntaxError,
929-
"f-string: expecting a valid expression after '{'",
934+
"f-string: lambda expression are not allowed "
935+
"without parentheses",
930936
["f'{lambda x:x}'",
931937
])
932938

Parser/parser.c

Lines changed: 34 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)