Skip to content

Commit

Permalink
Merge pull request #65 from lysnikolaou/more-fstring-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pablogsal authored Apr 9, 2023
2 parents 416fa9c + cb1e7ea commit 9a6e7e4
Show file tree
Hide file tree
Showing 5 changed files with 437 additions and 308 deletions.
6 changes: 3 additions & 3 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -1363,9 +1363,9 @@ invalid_replacement_field:
| '{' a='!' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: expression required before '!'") }
| '{' a='}' { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: empty expression not allowed") }
| '{' (yield_expr | star_expressions) "="? invalid_conversion_character
| '{' (yield_expr | star_expressions) "="? [ "!" NAME ] [ ':' fstring_format_spec* ] !'}' {
RAISE_SYNTAX_ERROR("f-string: expecting '}'")
}
# We explicitly require either a conversion character or a format spec (or both) in order for this to not get too general
| '{' (yield_expr | star_expressions) "="? "!" NAME [':' fstring_format_spec*] !'}' { RAISE_SYNTAX_ERROR("f-string: expecting '}'") }
| '{' (yield_expr | star_expressions) "="? ':' fstring_format_spec* !'}' { RAISE_SYNTAX_ERROR("f-string: expecting '}'") }
invalid_conversion_character:
| a="!" &(':'|'}') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: missed conversion character") }
| a="!" !NAME { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "f-string: invalid conversion character") }
2 changes: 1 addition & 1 deletion Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def baz():
{
6
0="""''', 6, 15)
0="""''', 5, 13)

# Errors thrown by symtable.c
check('x = [(yield i) for i in range(3)]', 1, 7)
Expand Down
10 changes: 4 additions & 6 deletions Lib/test/test_fstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ def test_lambda(self):

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

Expand Down Expand Up @@ -1258,8 +1258,6 @@ def test_mismatched_braces(self):
"f'{{{'",
"f'{{}}{'",
"f'{'",
"f'x{<'", # See bpo-46762.
"f'x{>'",
"f'{i='", # See gh-93418.
])

Expand Down Expand Up @@ -1483,7 +1481,7 @@ def test_walrus(self):
self.assertEqual(x, 10)

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

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

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

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

Expand Down
Loading

0 comments on commit 9a6e7e4

Please sign in to comment.