From e331da985b1d57095f10203b4bb92ef1dfe2b707 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 6 Jul 2026 16:49:17 +0300 Subject: [PATCH] Add tests for syntax error messages that had no test coverage (GH-153192) "illegal target for annotation" and "cannot use dict unpacking here" were not tested at all, and "f-string: expecting '!', or ':', or '}'" was only tested for its t-string variant. (cherry picked from commit 836b2069c86e25cf9456e9210bb612e5166d0779) Co-authored-by: Serhiy Storchaka Co-authored-by: Claude Fable 5 --- Lib/test/test_fstring.py | 3 +++ Lib/test/test_syntax.py | 10 ++++++++++ Lib/test/test_unpack_ex.py | 14 ++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py index 5cc02c30ec2ba33..9a8e56836fc671c 100644 --- a/Lib/test/test_fstring.py +++ b/Lib/test/test_fstring.py @@ -1706,6 +1706,9 @@ def test_invalid_syntax_error_message(self): with self.assertRaisesRegex(SyntaxError, "f-string: expecting '=', or '!', or ':', or '}'"): compile("f'{a $ b}'", "?", "exec") + with self.assertRaisesRegex(SyntaxError, + "f-string: expecting '!', or ':', or '}'"): + compile("f'{a=b}'", "?", "exec") def test_with_two_commas_in_format_specifier(self): error_msg = re.escape("Cannot specify ',' with ','.") diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py index 232dcb92ed2901f..7859387dccef376 100644 --- a/Lib/test/test_syntax.py +++ b/Lib/test/test_syntax.py @@ -2238,6 +2238,16 @@ Traceback (most recent call last): SyntaxError: only single target (not list) can be annotated +>>> 1: int +Traceback (most recent call last): +SyntaxError: illegal target for annotation +>>> -x: int = 1 +Traceback (most recent call last): +SyntaxError: illegal target for annotation +>>> (x for x in y): int +Traceback (most recent call last): +SyntaxError: illegal target for annotation + # 'not' after operators: >>> 3 + not 3 diff --git a/Lib/test/test_unpack_ex.py b/Lib/test/test_unpack_ex.py index 33c96b84964b591..4fe19765cacf110 100644 --- a/Lib/test/test_unpack_ex.py +++ b/Lib/test/test_unpack_ex.py @@ -355,6 +355,20 @@ ^^^ SyntaxError: cannot use dict unpacking in a dictionary value + >>> (**a) + Traceback (most recent call last): + ... + (**a) + ^^ + SyntaxError: cannot use dict unpacking here + + >>> {**a for a in {1: 2}.items(): b} + Traceback (most recent call last): + ... + {**a for a in {1: 2}.items(): b} + ^^ + SyntaxError: cannot use dict unpacking here + # Generator expression in function arguments