python / python/cpython

Overzealous Optimization For `isinstance`

未關閉
#144,873 8 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

docs
主要語言
Python
星號
77.2k
分支
35.9k
PR 合併指標
PR 指標待擷取

描述

Bug report

Bug description:

The custom __instancecheck__ dunder method allows full customization (described in the Data model).

However, it produces a spurious result:

class CustomIsInstanceMeta(type):
    def __instancecheck__(cls, instance: object) -> bool:
        return type(instance) is int

class CustomIsInstance(metaclass=CustomIsInstanceMeta):
    ...

class CustomIsInstanceSubclass(CustomIsInstance):
    ...


print(isinstance(5, CustomIsInstance)) # True
print(isinstance(CustomIsInstance(), CustomIsInstance)) # True (should be False)
print(isinstance(CustomIsInstanceSubclass(), CustomIsInstance)) # False

This is caused by:
https://github.com/python/cpython/blob/23c488d6197191d355be6733699a295c20806932/Objects/abstract.c#L2641-L2644
which is invalid in this case.

This is a reframing of #79264 (request to fix documentation)

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Linked PRs
  • gh-144874
  • gh-144997

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

檢查 Objects/abstract.c 的第 2641-2644 行以及連結的 PR gh-144874 和 gh-144997。在 Python 3.13/Linux 上執行提供的 isinstance 重現,以確認這些異常結果。完成的標準是已解決所回報的行為,且結果與 #79264 中提出的文件問題一致。

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

評估

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

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

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