[Python-bugs-list] [Bug #132313] error message confusing for assignment in lambda

noreply@sourceforge.net noreply@sourceforge.net
Sat, 17 Feb 2001 20:46:52 -0800


Bug #132313, was updated on 2001-Feb-14 00:45
Here is a current snapshot of the bug.

Project: Python
Category: Parser/Compiler
Status: Closed
Resolution: Fixed
Bug Group: None
Priority: 5
Submitted by: nobody
Assigned to : tim_one
Summary: error message confusing for assignment in lambda

Details: Python 2.1a1 (#4, Feb 11 2001, 16:05:58) 
[GCC 2.95.2 20000220 (Debian GNU/Linux)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> map(lambda foo: foo[0] = 4, [[1, 2]*3])
SyntaxError: keyword can't be an expression


Follow-Ups:

Date: 2001-Feb-17 20:46
By: tim_one

Comment:
I put in a hack to make this produce:
SyntaxError: lambda cannot contain assignment

This triggers if and only if the LHS test bottoms out at a lambdef.  Solves
nothing in general, but is a one-liner change that catches this kind of
case reliably, and hurts nothing else.  If the flawed lambda is embedded in
a larger expression, then this test won't trigger, but then "keyword can't
be an expression" is an *appropriate* msg.

compile.c rev 2.165.
-------------------------------------------------------

Date: 2001-Feb-14 06:42
By: gvanrossum

Comment:
Assigning to Tim Peters. My own inclination is to close this with a "Won't
Fix", because it's nearly impossible to get the parser to spit out a better
error message without completely reimplementing the parser using a
different approach. But Tim may want this to nag us more...

Background: while we would like the grammar for function arguments to be
[NAME '='] expression, our parser generator doesn't support that, and
unfortunately it has to have the form "expression ['=' expression]". Then
the code generator back-end does an additional check to make sure that if
the second expression is given (meaning this is a keyword argument), the
first expression is of the form NAME.  That's the error you're getting
here, because "lambda foo: foo[0]" happens to be a valid match for the
first expression.  I don't see an easy way to improve the error message.

-------------------------------------------------------

For detailed info, follow this link:
http://sourceforge.net/bugs/?func=detailbug&bug_id=132313&group_id=5470