Skip to content

bpo-46167: Allow assert statements with parentheses #30247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ del_stmt[stmt_ty]:

yield_stmt[stmt_ty]: y=yield_expr { _PyAST_Expr(y, EXTRA) }

assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _PyAST_Assert(a, b, EXTRA) }
assert_stmt[stmt_ty]:
| 'assert' '(' a=expression ',' b=expression [','] ')' &(NEWLINE | '&') { _PyAST_Assert(a, b, EXTRA) }
| 'assert' a=expression b=[',' z=expression { z }] { _PyAST_Assert(a, b, EXTRA) }

import_stmt[stmt_ty]: import_name | import_from

Expand Down
26 changes: 22 additions & 4 deletions Lib/test/test_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,9 @@ def test_assert(self):
assert 1, 1
assert lambda x:x
assert 1, lambda x:x+1
assert (1, "Oh no")
assert (1,
"Oh no")

try:
assert True
Expand Down Expand Up @@ -1304,12 +1307,27 @@ def test_assert_failures(self):
else:
self.fail("AssertionError not raised by 'assert False'")

try:
assert (0, "msg")
except AssertionError as e:
self.assertEqual(e.args[0], "msg")
else:
self.fail("AssertionError not raised by assert 0")

try:
assert (0,
"msg")
except AssertionError as e:
self.assertEqual(e.args[0], "msg")
else:
self.fail("AssertionError not raised by assert 0")

def test_assert_syntax_warnings(self):
# Ensure that we warn users if they provide a non-zero length tuple as
# the assertion test.
self.check_syntax_warning('assert(x, "msg")',
self.check_syntax_warning('assert(x, y, "msg")',
'assertion is always true')
self.check_syntax_warning('assert(False, "msg")',
self.check_syntax_warning('assert(False, 2, "msg")',
'assertion is always true')
self.check_syntax_warning('assert(False,)',
'assertion is always true')
Expand All @@ -1329,9 +1347,9 @@ def test_assert_warning_promotes_to_syntax_error(self):
except SyntaxError:
self.fail('SyntaxError incorrectly raised for \'assert x, "msg"\'')
with self.assertRaises(SyntaxError):
compile('assert(x, "msg")', '<testcase>', 'exec')
compile('assert(x, "msg", 3)', '<testcase>', 'exec')
with self.assertRaises(SyntaxError):
compile('assert(False, "msg")', '<testcase>', 'exec')
compile('assert(False, "msg", 4)', '<testcase>', 'exec')
with self.assertRaises(SyntaxError):
compile('assert(False,)', '<testcase>', 'exec')

Expand Down
Loading