python / python/cpython

Specialize syntax error for assignment expressions in invalid places

未關閉
#146,601 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

interpreter-core topic-parser type-feature
主要語言
Python
星號
77.2k
分支
35.9k
PR 合併指標
PR 指標待擷取

描述

Feature or enhancement

Proposal:

There are a few places where (unparenthesized) assignment expressions are not accepted but users might think of using them. For example:

>>> func(arg=x := foo())
  File "<python-input-0>", line 1
    func(arg=x := foo())
               ^^
SyntaxError: invalid syntax

I find this error pretty confusing. It's not clear why this is a syntax error or what users should do it fix it (i.e. add parentheses).

I propose adding a specialized syntax error to point users in the right direction, similar to what's currently done for assert:

>>> assert x := 1
  File "<python-input-12>", line 1
    assert x := 1
           ^^^^^^
SyntaxError: cannot use named expression without parentheses here

Refs #138716, #146260

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

先重現 issue 中的範例,並閱讀 CPython 對 assignment expressions 的語法錯誤處理,將其與現有針對 assert 的專用診斷進行比較。當無效的未加括號 assignment expressions 收到明確訊息、引導使用者使用括號,同時有效形式保留現有行為時,即表示完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
compilers
Issue 類型
功能
難度
4/5
預估耗時
3-5 天
活躍度
冷清
描述清晰度
基本清楚
新手友好度
58/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。