RustPython / RustPython/Parser

Illegal "yield (from)" python syntax accepted

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

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

bug
主要言語
Rust
スター
117
フォーク
38
平均マージ
4日 7時間
マージ済み PR(30日)
1

説明

Originally reported as https://github.com/astral-sh/ruff/issues/4992.

I'm not quite sure where the incorrectness here is, but I think this bug indicates an incorrect parsing of the yield from expression. The following is a python snippet with illegal syntax:

def e():
  x = yield from y = 5

We can verify that it is indeed illegal syntax with:

$ python yield-elision.py 
  File "/tmp/tmp.1z4PScoI6T/yield-elision.py", line 2
    x = yield from y = 5
        ^^^^^^^^^^^^
SyntaxError: assignment to yield expression not possible

Ruff, however, believes this is valid syntax:

$ ruff yield-elision.py --show-source --show-fixes
yield-elision.py:2:3: F841 [*] Local variable `x` is assigned to but never used
  |
2 | def e():
3 |   x = yield from y = 5
  |   ^ F841
  |
  = help: Remove assignment to unused variable `x`

yield-elision.py:2:18: F821 Undefined name `y`
  |
2 | def e():
3 |   x = yield from y = 5
  |                  ^ F821
  |

Found 2 errors.

And will attempt to fix it, leading to a self-reported parsing error:

$ ruff yield-elision.py  --fix
error: Autofix introduced a syntax error in `yield-elision.py` with rule codes F841: invalid syntax. Got unexpected token '=' at byte offset 24
---
def e():
  yield from y = 5

---
yield-elision.py:2:3: F841 Local variable `x` is assigned to but never used
yield-elision.py:2:18: F821 Undefined name `y`
Found 2 errors.

I believe this indicates a parsing bug.

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず issue の yield from の例を再現し、parser がその式をどのように処理するかを追跡します。Ruff が報告した無効な Python 構文が、有効なコードとして扱われたり、構文エラーを導入する autofix が生成されたりせず、拒否されれば完了です。

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

評価

技術スタック
python, rust
領域
compilers
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
45/100

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

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