TracebackException.lineno changes from str to int after format() when a keyword typo suggestion is found
維護者通常 1 天內回覆
評估
研究方向
從 TracebackException._find_keyword_typos() 著手,檢查它如何指定 lineno 和 end_lineno;issue 指出,建構子的字串轉換是預期行為。閱讀有關關鍵字拼寫錯誤建議的 traceback 測試,並補上格式化前後屬性型別的測試涵蓋。格式化保留文件所述或已確立的型別,且迴歸測試通過,即表示完成。
由索引模型根據 Issue 內容生成。
描述
Bug report
For a SyntaxError, TracebackException stores lineno and end_lineno as strings. When the keyword typo suggestion ("Did you mean 'while'?") triggers during formatting, both attributes are replaced with ints.
import sys
import traceback
print(sys.version)
try:
compile("whille True:\n pass\n", "example.py", "exec")
except SyntaxError as exc:
te = traceback.TracebackException.from_exception(exc)
print("before format():", repr(te.lineno), repr(te.end_lineno))
"".join(te.format())
print("after format(): ", repr(te.lineno), repr(te.end_lineno))
On main:
3.16.0a2+dev (remotes/origin/HEAD:d456158, Nov 19 2026, 22:08:35) [GCC 13.3.0]
before format(): '1' '1'
after format(): 1 1
On 3.14:
3.14.6 (main, Jul 18 2026, 17:01:31) [Clang 22.1.3 ]
before format(): '1' '1'
after format(): 1 1
On 3.13.18 both stay '1', since there are no keyword suggestions there.
The constructor does self.lineno = str(lno), but TracebackException._find_keyword_typos() later assigns self.lineno = start[0] and self.end_lineno = end[0], which are ints from the tokenizer.
The printed traceback is fine, so most people won't notice this. It only shows up if your code reads the attributes directly. For example, te.lineno == '1' is True before calling format() and False after.
It also affects equality: two TracebackException objects created from the same exception compare equal, but stop being equal after you format one of them.
I think the two assignments should be wrapped in str() to match the constructor. I'm happy to send a PR with a test if that's the right direction. The docs don't currently state the type of these attributes, so that could be clarified at the same time.
CPython versions tested on:
CPython main branch
Operating systems tested on:
macOS
- 主要語言
- Python
- 星號
- 78.4k
- 分支
- 36.6k
- 平均合併
- 23 小時 34 分鐘
- 30 天內合併 PR
- 610
環境準備
在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
python/cpython 的其他 Issue
-
interpreter-core type-bug
難度 2/5 1-3 小時 新手友好度 62/100
維護者通常 1 天內回覆
-
stdlib topic-subprocess type-bug
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 1 天內回覆
-
corner case: base64.b32decode ignorechars=b'=' miscounts padding可能已有人在做 @ElDShell 今天認領。 未關閉3.15 easy type-bug
難度 2/5 1-3 小時 新手友好度 62/100
python/cpython#162353 · 1 則留言 ·
維護者通常 1 天內回覆
-
[HACL] The `HACL_CAN_COMPILE_VEC128` macro is not defined when building Python with `-Wundef`可能已有人在做 @tamaki0575-cloud 於 1 天前認領。 未關閉build extension-modules topic-C-API type-bug
難度 2/5 1-3 小時 新手友好度 64/100
python/cpython#162219 · 2 則留言 ·
維護者通常 1 天內回覆
-
zipfile: ZipInfo.comment exceeding 65535 bytes causes struct.error in ZipFile.close() without proper validation可能已有人在做 @1cbyc 於 5 天前認領。 未關閉stdlib type-bug
難度 2/5 1-3 小時 新手友好度 75/100
維護者通常 1 天內回覆
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 68/100
vllm-project/vllm#69304 · 1 則留言 ·
維護者通常 1 天內回覆
-
pydanty:is-working
難度 2/5 1-3 小時 新手友好度 68/100
pydantic/pydantic-ai#14187 ·
維護者通常 1 天內回覆
-
bug
難度 2/5 1-3 小時 新手友好度 62/100
維護者通常 1 天內回覆
-
test
難度 2/5 1-3 小時 新手友好度 62/100
prebid/salesagent#2676 ·
維護者通常 1 天內回覆
-
難度 1/5 1-3 小時 新手友好度 62/100
sunososobro-hub/cloudball#339 ·
維護者通常 1 天內回覆