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:
|
/* Quick test for an exact match */ |
|
if (Py_IS_TYPE(inst, (PyTypeObject *)cls)) { |
|
return 1; |
|
} |
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
Bug report
Bug description:
The custom
__instancecheck__dunder method allows full customization (described in the Data model).However, it produces a spurious result:
This is caused by:
cpython/Objects/abstract.c
Lines 2641 to 2644 in 23c488d
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
isinstanceOptimization #144874