bpo-39176: Improve error message for 'named assignment' (GH-17777) (GH-17778)
(cherry picked from commit 37143a8e3b
)
Co-authored-by: Ned Batchelder <ned@nedbatchelder.com>
Co-authored-by: Ned Batchelder <ned@nedbatchelder.com>
This commit is contained in:
parent
302b35f82b
commit
6c004955ac
|
@ -32,7 +32,7 @@ class NamedExpressionInvalidTest(unittest.TestCase):
|
||||||
def test_named_expression_invalid_06(self):
|
def test_named_expression_invalid_06(self):
|
||||||
code = """((a, b) := (1, 2))"""
|
code = """((a, b) := (1, 2))"""
|
||||||
|
|
||||||
with self.assertRaisesRegex(SyntaxError, "cannot use named assignment with tuple"):
|
with self.assertRaisesRegex(SyntaxError, "cannot use assignment expressions with tuple"):
|
||||||
exec(code, {}, {})
|
exec(code, {}, {})
|
||||||
|
|
||||||
def test_named_expression_invalid_07(self):
|
def test_named_expression_invalid_07(self):
|
||||||
|
@ -90,7 +90,7 @@ class NamedExpressionInvalidTest(unittest.TestCase):
|
||||||
code = """(lambda: x := 1)"""
|
code = """(lambda: x := 1)"""
|
||||||
|
|
||||||
with self.assertRaisesRegex(SyntaxError,
|
with self.assertRaisesRegex(SyntaxError,
|
||||||
"cannot use named assignment with lambda"):
|
"cannot use assignment expressions with lambda"):
|
||||||
exec(code, {}, {})
|
exec(code, {}, {})
|
||||||
|
|
||||||
def test_named_expression_invalid_16(self):
|
def test_named_expression_invalid_16(self):
|
||||||
|
|
|
@ -45,7 +45,7 @@ SyntaxError: cannot assign to True
|
||||||
|
|
||||||
>>> (True := 1)
|
>>> (True := 1)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
SyntaxError: cannot use named assignment with True
|
SyntaxError: cannot use assignment expressions with True
|
||||||
|
|
||||||
>>> obj.__debug__ = 1
|
>>> obj.__debug__ = 1
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
|
|
|
@ -1955,7 +1955,7 @@ ast_for_namedexpr(struct compiling *c, const node *n)
|
||||||
if (target->kind != Name_kind) {
|
if (target->kind != Name_kind) {
|
||||||
const char *expr_name = get_expr_name(target);
|
const char *expr_name = get_expr_name(target);
|
||||||
if (expr_name != NULL) {
|
if (expr_name != NULL) {
|
||||||
ast_error(c, n, "cannot use named assignment with %s", expr_name);
|
ast_error(c, n, "cannot use assignment expressions with %s", expr_name);
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue