Misleading SyntaxError: cannot use assignment expressions with lambda
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug report
Bug description:
Python 3.12.8 (tags/v3.12.8:2dc476b, Dec 3 2024, 19:30:04) [MSC v.1942 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> lambda x: foo := 2
File "<stdin>", line 1
lambda x: foo := 2
^^^^^^^^^^^^^
SyntaxError: cannot use assignment expressions with lambda
>>> lambda x: (foo := 2)
<function <lambda> at 0x00000229CC40E0C0>
IIRC, until today I believed that you could never use an assignment expression in a lambda. I was disabused of this notion only by reading PEP 572 itself. I believe the SyntaxError should ideally read something along the lines of "cannot use unparenthesized assignment expressions with lambda".
I wasn't sure to what category to file this under exactly, sorry if I chose wrong.
CPython versions tested on:
3.12
Operating systems tested on:
Windows
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by searching CPython for the exact "cannot use assignment expressions with lambda" diagnostic and review the related parser or compiler handling, with PEP 572 as context. Reproduce both lambda examples from the report; done means the unparenthesized form reports the more precise restriction while the parenthesized form continues to work.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100