python / python/cpython

Misleading SyntaxError: cannot use assignment expressions with lambda

Open
#137,570 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core topic-parser type-bug
Dominant language
Python
Stars
77.2k
Forks
36k
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.