python / python/cpython

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

Aperta
#136,713 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

extension-modules stdlib type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo il riproduttore fornito su CPython 3.14, concentrandoti su ABC.subclasshook, _abc_instancecheck e sull’interazione tra Parent e _ParentCheck. Il lavoro è completo quando Test Set 1 non genera più RecursionError e Test Sets 2 e 3 continuano a superare i test.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.