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.

classification
Title: abc: `issubclass([], my_abstract_type)` raises exception
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.2
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, benjamin.peterson, cool-RR, michael.foord
Priority: normal Keywords:

Created on 2010-11-28 21:47 by cool-RR, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (7)
msg122736 - (view) Author: Ram Rachum (cool-RR) * Date: 2010-11-28 21:47
>>> import abc
>>> class A(object, metaclass=abc.ABCMeta):
...     pass
>>> issubclass([], A)
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    issubclass([], A)
  File "c:\Python32\lib\abc.py", line 137, in __subclasscheck__
    if subclass in cls._abc_cache:
  File "c:\Python32\lib\_weakrefset.py", line 69, in __contains__
    return ref(item) in self.data
TypeError: cannot create weak reference to 'list' object

I should be able to check whether an object is a subclass of something without confirming it's a type first. I think this call should just return False.
msg122737 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2010-11-28 21:58
I don't agree. "issubclass(1, list)" has always raised an exception.
msg122738 - (view) Author: Ram Rachum (cool-RR) * Date: 2010-11-28 22:01
But do you think it's a good idea that `issubclass(1, list)` raises an exception? Why not simply return `False`?

Do you think there's someone out there who's counting on `issubclass` to raise an exception?
msg122739 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2010-11-28 22:16
issubclass is for relationship between classes. Did you consider isinstance() instead?
msg122740 - (view) Author: Ram Rachum (cool-RR) * Date: 2010-11-28 22:22
Amaury, I am aware of what `issubclass` does and what `isinstance` does. I am not manually feeding `[]` into `issubclass`. I have an object which can be either a list, or a string, or a callable, or a type. And I want to check whether it's a sub-class of some base class.

So I don't think I should be taking extra precautions before using `issubclass`: If my object is not a subclass of the given base class, I should just get `False`.
msg122741 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2010-11-28 22:31
Tell python-ideas about it.
msg122743 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2010-11-28 22:42
FWIW (which isn't much I guess) it annoys me that I have to protect calls to issubclass with if isinstance(obj, type).
History
Date User Action Args
2022-04-11 14:57:09adminsetgithub: 54778
2010-11-28 22:42:23michael.foordsetnosy: + michael.foord
messages: + msg122743
2010-11-28 22:31:35benjamin.petersonsetstatus: open -> closed
nosy: + benjamin.peterson
messages: + msg122741

2010-11-28 22:22:16cool-RRsetmessages: + msg122740
2010-11-28 22:16:41amaury.forgeotdarcsetmessages: + msg122739
2010-11-28 22:01:54cool-RRsetstatus: closed -> open

messages: + msg122738
2010-11-28 21:58:56amaury.forgeotdarcsetstatus: open -> closed

nosy: + amaury.forgeotdarc
messages: + msg122737

resolution: not a bug
2010-11-28 21:47:58cool-RRcreate