Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4e30378

Browse files
committed
Bug #132313 error message confusing for assignment in lambda.
They're actually complaining about something more specific, an assignment in a lambda as an actual argument, so that Python parses the lambda as if it were a keyword argument. Like f(lambda x: x[0]=42). The "lambda x: x[0]" part gets parsed as if it were a keyword, being bound to 42, and the resulting error msg didn't make much sense.
1 parent 7834907 commit 4e30378

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

Python/compile.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1522,8 +1522,15 @@ com_argument(struct compiling *c, node *n, PyObject **pkeywords)
15221522
m = CHILD(m, 0);
15231523
} while (NCH(m) == 1);
15241524
if (TYPE(m) != NAME) {
1525+
/* f(lambda x: x[0] = 3) ends up getting parsed with
1526+
* LHS test = lambda x: x[0], and RHS test = 3.
1527+
* SF bug 132313 points out that complaining about a keyword
1528+
* then is very confusing.
1529+
*/
15251530
com_error(c, PyExc_SyntaxError,
1526-
"keyword can't be an expression");
1531+
TYPE(m) == lambdef ?
1532+
"lambda cannot contain assignment" :
1533+
"keyword can't be an expression");
15271534
}
15281535
else {
15291536
PyObject *v = PyString_InternFromString(STR(m));

0 commit comments

Comments
 (0)