Issue 10569: abc: issubclass([], my_abstract_type) raises exception (original) (raw)

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 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:09 admin set github: 54778
2010-11-28 22:42:23 michael.foord set nosy: + michael.foordmessages: +
2010-11-28 22:31:35 benjamin.peterson set status: open -> closednosy: + benjamin.petersonmessages: +
2010-11-28 22:22:16 cool-RR set messages: +
2010-11-28 22:16:41 amaury.forgeotdarc set messages: +
2010-11-28 22:01:54 cool-RR set status: closed -> openmessages: +
2010-11-28 21:58:56 amaury.forgeotdarc set status: open -> closednosy: + amaury.forgeotdarcmessages: + resolution: not a bug
2010-11-28 21:47:58 cool-RR create