classification
Title: isinstance(anything, MetaclassThatDefinesInstancecheck) raises instead of returning False
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.0, Python 2.6
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: ajaksu2, amaury.forgeotdarc, jyasskin, mato2000, werneck, zanella
Priority: normal Keywords: patch

Created on 2008-03-17 16:45 by jyasskin, last changed 2009-05-13 22:28 by ajaksu2. 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 "<stdin>", line 1, in <module>
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 "<stdin>", line 1, in <module>
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) Date: 2009-05-13 22:28
This now works correctly (returns False) in release26-maint, trunk and py3k.
History
Date User Action Args
2009-05-13 22:28:50ajaksu2setstatus: open -> closed

nosy: + ajaksu2
messages: + msg87718

resolution: out of date
stage: resolved
2008-06-30 12:45:01amaury.forgeotdarcsetfiles: + instancecheck.patch
nosy: + amaury.forgeotdarc
messages: + msg69005
2008-06-30 04:10:28jyasskinsetstatus: closed -> open
resolution: not a bug -> (no value)
messages: + msg68990
2008-06-30 03:05:08benjamin.petersonsetstatus: open -> closed
resolution: not a bug
2008-06-30 02:42:30mato2000setmessages: + msg68987
2008-06-30 01:17:58zanellasetnosy: + zanella
messages: + msg68985
2008-06-21 18:34:13mato2000setnosy: + mato2000
messages: + msg68532
2008-05-03 00:00:41wernecksetmessages: + msg66134
2008-05-02 23:20:49wernecksetfiles: - issue2325.patch
2008-05-02 22:56:52wernecksetfiles: + issue2325.patch
keywords: + patch
messages: + msg66129
nosy: + werneck
2008-03-17 16:45:06jyasskincreate