This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Unsupported provider

classification
Title: isinstance(... ,collections.Callable) fails with oldstyle class instances
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7, Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: flox Nosy List: benjamin.peterson, ezio.melotti, flox, pitrou, rgammans
Priority: critical Keywords: easy, patch

Created on 2010-01-03 19:13 by rgammans, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue7624_abc.diff flox, 2010-01-29 18:53 Patch, apply to trunk
Messages (7)
msg97175 - (view) Author: Roger Gammans (rgammans) Date: 2010-01-03 19:13
The following sequence causes isinstance to raise an exception rather than to return False.

>>> class foo:
...     pass
... 
>>> import collections
>>> isinstance(foo,collections.Callable)
True
>>> isinstance(foo(),collections.Callable)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/abc.py", line 131, in __instancecheck__
    return (cls.__subclasscheck__(subclass) or
  File "/usr/lib/python2.6/abc.py", line 147, in __subclasscheck__
    ok = cls.__subclasshook__(subclass)
  File "/usr/lib/python2.6/_abcoll.py", line 117, in __subclasshook__
    if any("__call__" in B.__dict__ for B in C.__mro__):
AttributeError: class foo has no attribute '__mro__'
>>>
msg98516 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-01-29 16:26
Confirmed with all others "isinstance(..., collections.Hashable)" and similar.

According to the documentation, we might expect the same behavior as for new-style class.
msg98517 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-01-29 17:19
Well the fix is easy for old-style classes, since we just have to use hasattr(obj, '__call__') in that case.
msg98522 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-01-29 18:38
Wow, critical issue, are you sure?

Here is the patch, with tests.
IMO, the tests may be ported to 3.x.
msg98524 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2010-01-29 18:46
"subtype == _InstanceType" can probably be replaced with "subtype is _InstanceType". Otherwise, the patch looks good to me.
msg98525 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-01-29 18:53
fixed
msg100652 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-03-08 15:35
Fixed in r78800.
Additional tests backported to 3.x.
History
Date User Action Args
2022-04-11 14:56:56adminsetgithub: 51873
2010-03-08 15:35:44floxsetstatus: open -> closed
title: isinstance(... ,collections.Callable) fails with oldstyle class i nstances -> isinstance(... ,collections.Callable) fails with oldstyle class instances
messages: + msg100652

resolution: accepted -> fixed
stage: commit review -> resolved
2010-02-27 16:57:11floxsetassignee: flox
resolution: accepted
stage: patch review -> commit review
2010-01-29 18:53:32floxsetfiles: + issue7624_abc.diff

messages: + msg98525
2010-01-29 18:53:01floxsetfiles: - issue7624_abc.diff
2010-01-29 18:46:36pitrousetnosy: + benjamin.peterson
messages: + msg98524
2010-01-29 18:38:07floxsetfiles: + issue7624_abc.diff
keywords: + patch
messages: + msg98522

stage: needs patch -> patch review
2010-01-29 17:19:06pitrousetpriority: normal -> critical

nosy: + pitrou
messages: + msg98517

keywords: + easy
2010-01-29 16:26:10floxsetpriority: normal
versions: + Python 2.7
messages: + msg98516

type: behavior
stage: needs patch
2010-01-29 14:05:47floxsetnosy: + flox
2010-01-07 21:53:33ezio.melottisetnosy: + ezio.melotti
2010-01-03 19:13:31rgammanscreate