Issue 2325: isinstance(anything, MetaclassThatDefinesInstancecheck) raises instead of returning False (original) (raw)

Created on 2008-03-17 16:45 by jyasskin, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
instancecheck.patch amaury.forgeotdarc,2008-06-30 12:44 patch for 2.6
Messages (9)
msg63671 - (view) Author: Jeffrey Yasskin (jyasskin) * (Python committer) Date: 2008-03-17 16:45
>>> class Meta(type): ... def __instancecheck__(self, other): ... return False >>> isinstance(3, Meta) In 2.6, this results in: Traceback (most recent call last): File "", line 1, in RuntimeError: maximum recursion depth exceeded while calling a Python object (That's a recursion in C, through PyObject_IsInstance and instancemethod_call) In 3.0, I get: Traceback (most recent call last): File "", line 1, in TypeError: __instancecheck__() takes exactly 2 positional arguments (1 given)
msg66129 - (view) Author: Pedro Werneck (werneck) Date: 2008-05-02 22:56
In 3.0 it happens with any class. Just the cls argument missing on the call to instancecheck.
msg66134 - (view) Author: Pedro Werneck (werneck) Date: 2008-05-03 00:00
Seems like that's the wrong usage and the PEP 3119 notices that it's hard to get the right semantics. To use it that way you need to define the methods as a classmethod(). http://www.python.org/dev/peps/pep-3119/#one-trick-ponies
msg68532 - (view) Author: Matias Gonzalez (mato2000) Date: 2008-06-21 18:34
This is not a bug. Function __instancecheck__ should be a classmethod. >>> class Meta(type): ... @classmethod ... def __instancecheck__(self, other): ... return False ... >>> isinstance(3, Meta) False
msg68985 - (view) Author: Rafael Zanella (zanella) Date: 2008-06-30 01:17
So..., could this issue be closed ?
msg68987 - (view) Author: Matias Gonzalez (mato2000) Date: 2008-06-30 02:42
Yes, it should be. I don't have permissions to.
msg68990 - (view) Author: Jeffrey Yasskin (jyasskin) * (Python committer) Date: 2008-06-30 04:10
I don't think so. It wouldn't be a bug if I wrote: >>> class Meta(type): ... def __instancecheck__(self, other): ... return True >>> isinstance(3, Meta) ... False but it is a bug that the isinstance call raises an exception. If recent builds no longer raise an exception, then the bug should be closed. You guys also seem to have missed that the examples in PEP 3119 in fact define __instancecheck__ as a normal method on a metaclass (which makes it a classmethod on classes derived from that metaclass) instead of as a classmethod on a metaclass.
msg69005 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-06-30 12:44
I think the best is to ignore __instancecheck__ when "cls.__instancecheck__" is returned as an unbound method. In this case, we try "type(cls).__instancecheck__" instead. Here is a patch along this idea. I had to disable a test named "Evil", because in this case isinstance() simply falls back to the original algorithm. I don't know if this is applicable to 3.0: unbound methods don't exist any more.
msg87718 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) Date: 2009-05-13 22:28
This now works correctly (returns False) in release26-maint, trunk and py3k.
History
Date User Action Args
2022-04-11 14:56:31 admin set github: 46578
2009-05-13 22:28:50 ajaksu2 set status: open -> closednosy: + ajaksu2messages: + resolution: out of datestage: resolved
2008-06-30 12:45:01 amaury.forgeotdarc set files: + instancecheck.patchnosy: + amaury.forgeotdarcmessages: +
2008-06-30 04:10:28 jyasskin set status: closed -> openresolution: not a bug -> (no value)messages: +
2008-06-30 03:05:08 benjamin.peterson set status: open -> closedresolution: not a bug
2008-06-30 02:42:30 mato2000 set messages: +
2008-06-30 01:17:58 zanella set nosy: + zanellamessages: +
2008-06-21 18:34:13 mato2000 set nosy: + mato2000messages: +
2008-05-03 00:00:41 werneck set messages: +
2008-05-02 23:20:49 werneck set files: - issue2325.patch
2008-05-02 22:56:52 werneck set files: + issue2325.patchkeywords: + patchmessages: + nosy: + werneck
2008-03-17 16:45:06 jyasskin create