RustPython / RustPython/Parser

Illegal "yield (from)" python syntax accepted

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

還沒有人認領這個 Issue。

bug
主要語言
Rust
星號
117
分支
38
平均合併
4 天 7 小時
30 天內合併 PR
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. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

首先重現 issue 中的 yield from 範例,並追蹤 parser 如何處理該表達式。完成的標準是:Ruff 回報的無效 Python 語法會被拒絕,而不是被當作有效程式碼處理,或產生引入語法錯誤的 autofix。

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

評估

技術堆疊
python, rust
領域
compilers
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
描述清楚
新手友好度
45/100

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

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