Skip to content

Commit 9a6e7e4

Browse files
authored
Merge pull request #65 from lysnikolaou/more-fstring-errors
2 parents 416fa9c + cb1e7ea commit 9a6e7e4

File tree

5 files changed

+437
-308
lines changed

5 files changed

+437
-308
lines changed

Grammar/python.gram

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,9 +1363,9 @@ invalid_replacement_field:
13631363
| '{' a='!' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: expression required before '!'") }
13641364
| '{' a='}' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: empty expression not allowed") }
13651365
| '{' (yield_expr | star_expressions) "="? invalid_conversion_character
1366-
| '{' (yield_expr | star_expressions) "="? [ "!" NAME ] [ ':' fstring_format_spec* ] !'}' {
1367-
RAISE_SYNTAX_ERROR("f-string: expecting '}'")
1368-
}
1366+
# We explicitly require either a conversion character or a format spec (or both) in order for this to not get too general
1367+
| '{' (yield_expr | star_expressions) "="? "!" NAME [':' fstring_format_spec*] !'}' { RAISE_SYNTAX_ERROR("f-string: expecting '}'") }
1368+
| '{' (yield_expr | star_expressions) "="? ':' fstring_format_spec* !'}' { RAISE_SYNTAX_ERROR("f-string: expecting '}'") }
13691369
invalid_conversion_character:
13701370
| a="!" &(':'|'}') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: missed conversion character") }
13711371
| a="!" !NAME { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: invalid conversion character") }

Lib/test/test_exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ def baz():
294294
295295
{
296296
6
297-
0="""''', 6, 15)
297+
0="""''', 5, 13)
298298

299299
# Errors thrown by symtable.c
300300
check('x = [(yield i) for i in range(3)]', 1, 7)

Lib/test/test_fstring.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ def test_lambda(self):
931931

932932
# lambda doesn't work without parens, because the colon
933933
# makes the parser think it's a format_spec
934-
self.assertAllRaise(SyntaxError, 'f-string: invalid syntax',
934+
self.assertAllRaise(SyntaxError, 'invalid syntax',
935935
["f'{lambda x:x}'",
936936
])
937937

@@ -1258,8 +1258,6 @@ def test_mismatched_braces(self):
12581258
"f'{{{'",
12591259
"f'{{}}{'",
12601260
"f'{'",
1261-
"f'x{<'", # See bpo-46762.
1262-
"f'x{>'",
12631261
"f'{i='", # See gh-93418.
12641262
])
12651263

@@ -1483,7 +1481,7 @@ def test_walrus(self):
14831481
self.assertEqual(x, 10)
14841482

14851483
def test_invalid_syntax_error_message(self):
1486-
with self.assertRaisesRegex(SyntaxError, "f-string: invalid syntax"):
1484+
with self.assertRaisesRegex(SyntaxError, "invalid syntax"):
14871485
compile("f'{a $ b}'", "?", "exec")
14881486

14891487
def test_with_two_commas_in_format_specifier(self):
@@ -1507,11 +1505,11 @@ def test_with_an_underscore_and_a_comma_in_format_specifier(self):
15071505
f'{1:_,}'
15081506

15091507
def test_syntax_error_for_starred_expressions(self):
1510-
error_msg = re.escape("cannot use starred expression here")
1508+
error_msg = re.escape("can't use starred expression here")
15111509
with self.assertRaisesRegex(SyntaxError, error_msg):
15121510
compile("f'{*a}'", "?", "exec")
15131511

1514-
error_msg = re.escape("cannot use double starred expression here")
1512+
error_msg = re.escape("invalid syntax")
15151513
with self.assertRaisesRegex(SyntaxError, error_msg):
15161514
compile("f'{**a}'", "?", "exec")
15171515

0 commit comments

Comments
 (0)