Behavior change for `foo and 1 or 2`: 3.12 newly converts `foo` to bool twice
未關閉
還沒有人認領這個 Issue。
3.14
docs
interpreter-core
type-bug
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 35.9k
- PR 合併指標
- PR 指標待擷取
描述
Bug report
Bug description:
We noticed a behavior change between 3.11 and 3.12. The following code calls Foo.__bool__ once in 3.11 and twice in 3.12. Consequently for this contrived example, the expression evaluates to different results in 3.11 and 3.12.
class Foo:
def __init__(self):
self._a = True
def __bool__(self):
self._a = not self._a
print(f"Foo.__bool__ -> {self._a}")
return self._a
Foo() and "a string" or 42
In Python 3.11:
>>> Foo() and "a string" or 42
Foo.__bool__ -> False
42
In Python 3.12 (and 3.13.0b2):
>>> Foo() and "a string" or 42
Foo.__bool__ -> False
Foo.__bool__ -> True
<__main__.Foo object at 0x7f0ae554c1a0>
Is this change intentional?
Note that I'm not necessarily asking to change this. We should arguably change the code to "a string" if Foo() else 42, which evaluates the same in 3.11 and 3.12.
CPython versions tested on:
3.12
Operating systems tested on:
Linux
Linked PRs
- gh-124394
- gh-124723
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
在 Python 3.11、3.12 和 3.13 上執行提供的 Foo.__bool__ reproducer,然後檢視相關聯的 PR gh-124394 和 gh-124723,以了解目前的方向。完成的標準是:行為變更已獲得解決或明確記錄,有回歸測試涵蓋,且預期結果清楚明確。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- compilers
- Issue 類型
- 缺陷
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 需要釐清
- 新手友好度
- 20/100