-
-
Notifications
You must be signed in to change notification settings - Fork 34.1k
Open
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
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)) # FalseThis is caused by:
Lines 2641 to 2644 in 23c488d
| /* 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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error