python / python/cpython

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

Đang mở
#136,713 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

extension-modules stdlib type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách chạy reproducer được cung cấp trên CPython 3.14, tập trung vào ABC.subclasshook, _abc_instancecheck và sự tương tác giữa Parent với _ParentCheck. Được xem là hoàn thành khi Test Set 1 không còn phát sinh RecursionError và Test Sets 2 và 3 tiếp tục vượt qua.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
compilers
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.