python / python/cpython

Overzealous Optimization For `isinstance`

オープン
#144,873 コメント 8 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Objects/abstract.c の 2641-2644 行と、リンクされている PR gh-144874 および gh-144997 を確認してください。提供されている isinstance の再現を Python 3.13/Linux で実行し、誤った結果を確認してください。報告された動作が解決され、結果が #79264 で提起されたドキュメント上の懸念と整合していれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
c, python
領域
backend
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。