bpo-35494: Improve syntax error messages for unbalanced parentheses in f-string. by serhiy-storchaka · Pull Request #11161 · python/cpython
Expand Up
@@ -368,9 +368,27 @@ def test_unterminated_string(self):
])
def test_mismatched_parens(self): self.assertAllRaise(SyntaxError, 'f-string: mismatched', self.assertAllRaise(SyntaxError, r"f-string: closing parenthesis '\}' " r"does not match opening parenthesis '\('", ["f'{((}'", ]) self.assertAllRaise(SyntaxError, r"f-string: closing parenthesis '\)' " r"does not match opening parenthesis '\['", ["f'{a[4)}'", ]) self.assertAllRaise(SyntaxError, r"f-string: closing parenthesis '\]' " r"does not match opening parenthesis '\('", ["f'{a(4]}'", ]) self.assertAllRaise(SyntaxError, r"f-string: closing parenthesis '\}' " r"does not match opening parenthesis '\['", ["f'{a[4}'", ]) self.assertAllRaise(SyntaxError, r"f-string: closing parenthesis '\}' " r"does not match opening parenthesis '\('", ["f'{a(4}'", ]) self.assertRaises(SyntaxError, eval, "f'{" + "("*500 + "}'")
def test_double_braces(self): self.assertEqual(f'{{', '{') Expand Down Expand Up @@ -448,7 +466,9 @@ def test_comments(self): ["f'{1#}'", # error because the expression becomes "(1#)" "f'{3(#)}'", "f'{#}'", "f'{)#}'", # When wrapped in parens, this becomes ]) self.assertAllRaise(SyntaxError, r"f-string: unmatched '\)'", ["f'{)#}'", # When wrapped in parens, this becomes # '()#)'. Make sure that doesn't compile. ])
Expand Down Expand Up @@ -577,7 +597,7 @@ def test_parens_in_expressions(self): "f'{,}'", # this is (,), which is an error ])
self.assertAllRaise(SyntaxError, "f-string: expecting '}'", self.assertAllRaise(SyntaxError, r"f-string: unmatched '\)'", ["f'{3)+(4}'", ])
Expand Down Expand Up @@ -1003,16 +1023,6 @@ def test_str_format_differences(self): self.assertEqual('{d[a]}'.format(d=d), 'string') self.assertEqual('{d[0]}'.format(d=d), 'integer')
def test_invalid_expressions(self): self.assertAllRaise(SyntaxError, r"closing parenthesis '\)' does not match " r"opening parenthesis '\[' \(<fstring>, line 1\)", [r"f'{a[4)}'"]) self.assertAllRaise(SyntaxError, r"closing parenthesis '\]' does not match " r"opening parenthesis '\(' \(<fstring>, line 1\)", [r"f'{a(4]}'"])
def test_errors(self): # see issue 26287 self.assertAllRaise(TypeError, 'unsupported', Expand Down
def test_mismatched_parens(self): self.assertAllRaise(SyntaxError, 'f-string: mismatched', self.assertAllRaise(SyntaxError, r"f-string: closing parenthesis '\}' " r"does not match opening parenthesis '\('", ["f'{((}'", ]) self.assertAllRaise(SyntaxError, r"f-string: closing parenthesis '\)' " r"does not match opening parenthesis '\['", ["f'{a[4)}'", ]) self.assertAllRaise(SyntaxError, r"f-string: closing parenthesis '\]' " r"does not match opening parenthesis '\('", ["f'{a(4]}'", ]) self.assertAllRaise(SyntaxError, r"f-string: closing parenthesis '\}' " r"does not match opening parenthesis '\['", ["f'{a[4}'", ]) self.assertAllRaise(SyntaxError, r"f-string: closing parenthesis '\}' " r"does not match opening parenthesis '\('", ["f'{a(4}'", ]) self.assertRaises(SyntaxError, eval, "f'{" + "("*500 + "}'")
def test_double_braces(self): self.assertEqual(f'{{', '{') Expand Down Expand Up @@ -448,7 +466,9 @@ def test_comments(self): ["f'{1#}'", # error because the expression becomes "(1#)" "f'{3(#)}'", "f'{#}'", "f'{)#}'", # When wrapped in parens, this becomes ]) self.assertAllRaise(SyntaxError, r"f-string: unmatched '\)'", ["f'{)#}'", # When wrapped in parens, this becomes # '()#)'. Make sure that doesn't compile. ])
Expand Down Expand Up @@ -577,7 +597,7 @@ def test_parens_in_expressions(self): "f'{,}'", # this is (,), which is an error ])
self.assertAllRaise(SyntaxError, "f-string: expecting '}'", self.assertAllRaise(SyntaxError, r"f-string: unmatched '\)'", ["f'{3)+(4}'", ])
Expand Down Expand Up @@ -1003,16 +1023,6 @@ def test_str_format_differences(self): self.assertEqual('{d[a]}'.format(d=d), 'string') self.assertEqual('{d[0]}'.format(d=d), 'integer')
def test_invalid_expressions(self): self.assertAllRaise(SyntaxError, r"closing parenthesis '\)' does not match " r"opening parenthesis '\[' \(<fstring>, line 1\)", [r"f'{a[4)}'"]) self.assertAllRaise(SyntaxError, r"closing parenthesis '\]' does not match " r"opening parenthesis '\(' \(<fstring>, line 1\)", [r"f'{a(4]}'"])
def test_errors(self): # see issue 26287 self.assertAllRaise(TypeError, 'unsupported', Expand Down