Skip to content

Commit 836b206

Browse files
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. Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1 parent 1034e73 commit 836b206

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

Lib/test/test_fstring.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,6 +1706,9 @@ def test_invalid_syntax_error_message(self):
17061706
with self.assertRaisesRegex(SyntaxError,
17071707
"f-string: expecting '=', or '!', or ':', or '}'"):
17081708
compile("f'{a $ b}'", "?", "exec")
1709+
with self.assertRaisesRegex(SyntaxError,
1710+
"f-string: expecting '!', or ':', or '}'"):
1711+
compile("f'{a=b}'", "?", "exec")
17091712

17101713
def test_with_two_commas_in_format_specifier(self):
17111714
error_msg = re.escape("Cannot specify ',' with ','.")

Lib/test/test_syntax.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2238,6 +2238,16 @@
22382238
Traceback (most recent call last):
22392239
SyntaxError: only single target (not list) can be annotated
22402240
2241+
>>> 1: int
2242+
Traceback (most recent call last):
2243+
SyntaxError: illegal target for annotation
2244+
>>> -x: int = 1
2245+
Traceback (most recent call last):
2246+
SyntaxError: illegal target for annotation
2247+
>>> (x for x in y): int
2248+
Traceback (most recent call last):
2249+
SyntaxError: illegal target for annotation
2250+
22412251
# 'not' after operators:
22422252
22432253
>>> 3 + not 3

Lib/test/test_unpack_ex.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,20 @@
355355
^^^
356356
SyntaxError: cannot use dict unpacking in a dictionary value
357357
358+
>>> (**a)
359+
Traceback (most recent call last):
360+
...
361+
(**a)
362+
^^
363+
SyntaxError: cannot use dict unpacking here
364+
365+
>>> {**a for a in {1: 2}.items(): b}
366+
Traceback (most recent call last):
367+
...
368+
{**a for a in {1: 2}.items(): b}
369+
^^
370+
SyntaxError: cannot use dict unpacking here
371+
358372
359373
# Generator expression in function arguments
360374

0 commit comments

Comments
 (0)