Keyword typo suggestions differ between file and string tokenizers
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- PR 合併指標
- PR 指標待擷取
描述
Bug report
Bug description:
Keyword-typo SyntaxError suggestions depend on whether CPython processes the same source using the file tokenizer or the string tokenizer.
Create t.py containing:
a=b=c=d=e=f=g=h=i=0
def fn():
retrun True
Running the file directly:
$ python t.py
uses the file-tokenizer path and produces the helpful diagnostic:
File ".../t.py", line 3
retrun True
^^^^^^
SyntaxError: invalid syntax. Did you mean 'return'?
Running the same file as a module:
$ python -m t
uses the string-tokenizer path and produces:
File ".../t.py", line 3
retrun True
^^^^
SyntaxError: invalid syntax
The same discrepancy is visible in the default PyREPL-based REPL.
Pasting the complete block as one input:
a=b=c=d=e=f=g=h=i=0
def fn():
retrun True
produces the generic error:
File "<python-input-0>", line 3
retrun True
^^^^
SyntaxError: invalid syntax
Entering and submitting the assignment separately, followed by the function:
a=b=c=d=e=f=g=h=i=0
def fn():
retrun True
produces:
File "<python-input-2>", line 2
retrun True
^^^^^^
SyntaxError: invalid syntax. Did you mean 'return'?
I would expect the same source code to produce the same keyword suggestion regardless of whether it is:
- executed as a file with
python t.py; - loaded as a module with
python -m t; - pasted into PyREPL as one block; or
- entered into PyREPL statement by statement.
This appears related to the keyword-typo suggestions added in #132449.
traceback.TracebackException._find_keyword_typos() treats the two source paths differently:
- When the source is loaded from the filename, tokens outside the reported error line are skipped before consuming the token-search budget.
- When the source is included directly in the
SyntaxErrormetadata, all non-keywordNAMEtokens in the extracted source fragment consume that budget.
The search is limited to ten such tokens. In this example, a through i account for nine tokens and fn is the tenth, so the string-source path stops before examining retrun. The file-source path skips those earlier lines and finds the typo.
One possible approach would be to prioritize tokens on the reported error line for both source paths, and only then search the surrounding source fragment. That would preserve the existing work limit while avoiding this tokenizer-dependent result.
CPython versions tested on:
CPython main branch, 3.15, 3.14
Operating systems tested on:
macOS
Linked PRs
- gh-156087
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 traceback.TracebackException._find_keyword_typos() 開始,比對其 filename 和 SyntaxError 中繼資料路徑,以及此處描述的十個 token 預算。重現上面的四種執行模式;當相同的原始碼在檔案執行、模組載入和兩種 PyREPL 輸入形式下產生相同的關鍵字建議時,即表示完成。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- compilers
- Issue 類型
- 缺陷
- 難度
- 3/5
- 預估耗時
- 1-2 天
- 活躍度
- 停滯
- 描述清晰度
- 描述清楚
- 新手友好度
- 25/100