python / python/cpython

Side-effect of `ABC.__subclasshook__` / `_abc_instancecheck` ▶ `isinstance(..., Parent)` calls `__subclasscheck__` of uninvolved Child class -> RecursionError

未關閉
#136,713 3 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

extension-modules stdlib type-bug
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

Bug report

Bug description:

I realized there is strange error of __subclasshook__, or possibly _abc_instancecheck which creates a side-effect that __subclasshook__ is called on a Child class that is not even queried.

What is even more strange this error only occurs when the crucial statement B is called in a AB situation, just B or BAB is fine.

# At the end of his snippet are 3 test sets, Only the first causes an error the other 2 are fine.
import sys
sys.setrecursionlimit(59)
from abc import ABC


class Base(ABC): ...

class _ParentCheck(Base):

    @classmethod
    def __subclasshook__(cls, subclass):
        if not issubclass(subclass, Base):
            return NotImplemented
        if not hasattr(subclass, "f"):
            return False
        return True


class Parent(Base):
    f: int

    @classmethod
    def construct(cls, foo: int):
        class Child(cls, _ParentCheck):
            f = foo
        
        
        return Child


# Test Set 1: Error

assert issubclass(Parent.construct(1), Parent)  # Statement A
assert not isinstance(lambda: None, Parent)  # Statement B

# Test Set 2: OK

assert not isinstance(lambda: None, Parent)  # Statement B

# Test Set 3: OK

assert not isinstance(lambda: None, Parent)  # Statement B
assert issubclass(Parent.construct(1), Parent)  # Statement A
assert not isinstance(lambda: None, Parent)  # Statement B

CPython versions tested on:

3.14

Operating systems tested on:

Linux

貢獻指南

開啟貢獻指南

從這裡開始

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

研究方向

首先在 CPython 3.14 上執行提供的重現程式,重點關注 ABC.subclasshook、_abc_instancecheck,以及 Parent 與 _ParentCheck 之間的互動。當 Test Set 1 不再引發 RecursionError,且 Test Sets 2 和 3 繼續通過時,即表示完成。

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

評估

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

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

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