Skip to content

Commit b4a1c55

Browse files
Update tests and whatsnew entry
1 parent a33a705 commit b4a1c55

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

Doc/whatsnew/3.14.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ Improved error messages
183183
error message highlights where the :token:`~python-grammar:expression` is
184184
required. (Contributed by Sergey Miryanov in :gh:`129515`.)
185185

186-
.. code-block:: python
186+
.. code-block:: pycon
187187
188188
>>> x = 1 if True else pass
189189
Traceback (most recent call last):

Lib/test/test_syntax.py

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2877,32 +2877,42 @@ def test_match_stmt_invalid_as_expr(self):
28772877

28782878
def test_ifexp_else_stmt(self):
28792879
msg = "expected expression after 'else', but statement is given"
2880-
self._check_error("x = 1 if 1 else pass", msg)
2881-
self._check_error("x = 1 if 1 else return", msg)
2882-
self._check_error("x = 1 if 1 else return 2", msg)
2883-
self._check_error("x = 1 if 1 else raise Exception('a')", msg)
2884-
self._check_error("x = 1 if 1 else del a", msg)
2885-
self._check_error("x = 1 if 1 else yield 2", msg)
2886-
self._check_error("x = 1 if 1 else assert False", msg)
2887-
self._check_error("x = 1 if 1 else break", msg)
2888-
self._check_error("x = 1 if 1 else continue", msg)
2889-
self._check_error("x = 1 if 1 else import", msg)
2890-
self._check_error("x = 1 if 1 else import ast", msg)
2891-
self._check_error("x = 1 if 1 else from", msg)
2892-
self._check_error("x = 1 if 1 else from ast import *", msg)
2880+
2881+
for stmt in [
2882+
"pass",
2883+
"return",
2884+
"return 2",
2885+
"raise Exception('a')",
2886+
"del a",
2887+
"yield 2",
2888+
"assert False",
2889+
"break",
2890+
"continue",
2891+
"import",
2892+
"import ast",
2893+
"from",
2894+
"from ast import *"
2895+
]:
2896+
self._check_error(f"x = 1 if 1 else {stmt}", msg)
28932897

28942898
def test_ifexp_body_stmt_else_expression(self):
28952899
msg = "expected expression before 'if', but statement is given"
2896-
self._check_error("x = pass if 1 else 1", msg)
2897-
self._check_error("x = break if 1 else 1", msg)
2898-
self._check_error("x = continue if 1 else 1", msg)
2900+
2901+
for stmt in [
2902+
"pass",
2903+
"break",
2904+
"continue"
2905+
]:
2906+
self._check_error(f"x = {stmt} if 1 else 1", msg)
28992907

29002908
def test_ifexp_body_stmt_else_stmt(self):
29012909
msg = "expected expression before 'if', but statement is given"
2902-
self._check_error("x = pass if 1 else pass", msg)
2903-
self._check_error("x = break if 1 else pass", msg)
2904-
self._check_error("x = continue if 1 else pass", msg)
2905-
self._check_error("x = continue if 1 else import ast", msg)
2910+
for lhs_stmt, rhs_stmt in [
2911+
("pass", "pass"),
2912+
("break", "pass"),
2913+
("continue", "import ast")
2914+
]:
2915+
self._check_error(f"x = {lhs_stmt} if 1 else {rhs_stmt}", msg)
29062916

29072917
def load_tests(loader, tests, pattern):
29082918
tests.addTest(doctest.DocTestSuite())

0 commit comments

Comments
 (0)