bpo-41084: Adjust message when an f-string expression causes a SyntaxError (GH-21084)
Prefix the error message with `fstring: `, when parsing an f-string expression throws a `SyntaxError`.
diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py
index 7ffe01d..0dc7dd8 100644
--- a/Lib/test/test_fstring.py
+++ b/Lib/test/test_fstring.py
@@ -524,7 +524,7 @@
# This looks like a nested format spec.
])
- self.assertAllRaise(SyntaxError, "invalid syntax",
+ self.assertAllRaise(SyntaxError, "f-string: invalid syntax",
[# Invalid syntax inside a nested spec.
"f'{4:{/5}}'",
])
@@ -598,7 +598,7 @@
# are added around it. But we shouldn't go from an invalid
# expression to a valid one. The added parens are just
# supposed to allow whitespace (including newlines).
- self.assertAllRaise(SyntaxError, 'invalid syntax',
+ self.assertAllRaise(SyntaxError, 'f-string: invalid syntax',
["f'{,}'",
"f'{,}'", # this is (,), which is an error
])
@@ -716,7 +716,7 @@
# lambda doesn't work without parens, because the colon
# makes the parser think it's a format_spec
- self.assertAllRaise(SyntaxError, 'invalid syntax',
+ self.assertAllRaise(SyntaxError, 'f-string: invalid syntax',
["f'{lambda x:x}'",
])
@@ -1194,6 +1194,10 @@
self.assertEqual(f'{(x:=10)}', '10')
self.assertEqual(x, 10)
+ def test_invalid_syntax_error_message(self):
+ with self.assertRaisesRegex(SyntaxError, "f-string: invalid syntax"):
+ compile("f'{a $ b}'", "?", "exec")
+
if __name__ == '__main__':
unittest.main()