python / python/cpython

Specialize syntax error for assignment expressions in invalid places

オープン
#146,601 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず issue の例を再現し、assignment expressions に対する CPython の構文エラー処理を読み、assert に対して既存の専用診断と比較します。未かっこの無効な assignment expressions に、かっこの使用を促す明確なメッセージが表示され、有効な形式では既存の動作が維持されれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
compilers
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
58/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。