bpo-43797: Improve syntax error for invalid comparisons (#25317) · python/cpython@b86ed8e · GitHub
Skip to content

Commit b86ed8e

Browse files
bpo-43797: Improve syntax error for invalid comparisons (#25317)
* bpo-43797: Improve syntax error for invalid comparisons * Update Lib/test/test_fstring.py Co-authored-by: Guido van Rossum <gvanrossum@gmail.com> * Apply review comments * can't -> cannot Co-authored-by: Guido van Rossum <gvanrossum@gmail.com>
1 parent 2459b92 commit b86ed8e

12 files changed

Lines changed: 1261 additions & 665 deletions

File tree

Grammar/python.gram

Lines changed: 24 additions & 9 deletions

Lib/test/test_cmd_line_script.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,8 @@ def test_syntaxerror_unindented_caret_position(self):
600600
script_name = _make_test_script(script_dir, 'script', script)
601601
exitcode, stdout, stderr = assert_python_failure(script_name)
602602
text = io.TextIOWrapper(io.BytesIO(stderr), 'ascii').read()
603-
# Confirm that the caret is located under the first 1 character
604-
self.assertIn("\n 1 + 1 = 2\n ^", text)
603+
# Confirm that the caret is located under the '=' sign
604+
self.assertIn("\n 1 + 1 = 2\n ^\n", text)
605605

606606
def test_syntaxerror_indented_caret_position(self):
607607
script = textwrap.dedent("""\
@@ -613,7 +613,7 @@ def test_syntaxerror_indented_caret_position(self):
613613
exitcode, stdout, stderr = assert_python_failure(script_name)
614614
text = io.TextIOWrapper(io.BytesIO(stderr), 'ascii').read()
615615
# Confirm that the caret is located under the first 1 character
616-
self.assertIn("\n 1 + 1 = 2\n ^", text)
616+
self.assertIn("\n 1 + 1 = 2\n ^\n", text)
617617

618618
# Try the same with a form feed at the start of the indented line
619619
script = (
@@ -624,7 +624,7 @@ def test_syntaxerror_indented_caret_position(self):
624624
exitcode, stdout, stderr = assert_python_failure(script_name)
625625
text = io.TextIOWrapper(io.BytesIO(stderr), "ascii").read()
626626
self.assertNotIn("\f", text)
627-
self.assertIn("\n 1 + 1 = 2\n ^", text)
627+
self.assertIn("\n 1 + 1 = 2\n ^\n", text)
628628

629629
def test_syntaxerror_multi_line_fstring(self):
630630
script = 'foo = f"""{}\nfoo"""\n'

Lib/test/test_codeop.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,6 @@ def test_invalid(self):
275275
ai("a = 'a\\\n")
276276

277277
ai("a = 1","eval")
278-
ai("a = (","eval")
279278
ai("]","eval")
280279
ai("())","eval")
281280
ai("[}","eval")

Lib/test/test_exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def baz():
260260
check('[*x for x in xs]', 1, 2)
261261
check('foo(x for x in range(10), 100)', 1, 5)
262262
check('for 1 in []: pass', 1, 5)
263-
check('(yield i) = 2', 1, 2)
263+
check('(yield i) = 2', 1, 11)
264264
check('def f(*):\n pass', 1, 8)
265265

266266
@cpython_only

Lib/test/test_fstring.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ def test_conversions(self):
990990
])
991991

992992
def test_assignment(self):
993-
self.assertAllRaise(SyntaxError, 'invalid syntax',
993+
self.assertAllRaise(SyntaxError, r'invalid syntax',
994994
["f'' = 3",
995995
"f'{0}' = x",
996996
"f'{x}' = x",
@@ -1276,11 +1276,11 @@ def test_with_an_underscore_and_a_comma_in_format_specifier(self):
12761276
f'{1:_,}'
12771277

12781278
def test_syntax_error_for_starred_expressions(self):
1279-
error_msg = re.escape("can't use starred expression here")
1279+
error_msg = re.escape("cannot use starred expression here")
12801280
with self.assertRaisesRegex(SyntaxError, error_msg):
12811281
compile("f'{*a}'", "?", "exec")
12821282

1283-
error_msg = re.escape("can't use double starred expression here")
1283+
error_msg = re.escape("cannot use double starred expression here")
12841284
with self.assertRaisesRegex(SyntaxError, error_msg):
12851285
compile("f'{**a}'", "?", "exec")
12861286

Lib/test/test_generators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2013,7 +2013,7 @@ def printsolution(self, x):
20132013
>>> def f(): (yield bar) = y
20142014
Traceback (most recent call last):
20152015
...
2016-
SyntaxError: cannot assign to yield expression
2016+
SyntaxError: cannot assign to yield expression here. Maybe you meant '==' instead of '='?
20172017
20182018
>>> def f(): (yield bar) += y
20192019
Traceback (most recent call last):

Lib/test/test_genexps.py

Lines changed: 1 addition & 1 deletion

0 commit comments

Comments
 (0)