[issue44932] `issubclass` and `isinstance` doesn't check for all 2nd argument types

Ken Jin report at bugs.python.org
Tue Aug 17 05:31:53 EDT 2021


Ken Jin <kenjin4096 at gmail.com> added the comment:

@Paul, Steve, Tim and Zach, I've removed y'all from nosy as this isn't a Windows issue (but a typing/builtin types one). If you feel that I shouldn't have done that: my apologies and please do tell me.

To summarize what OP said:

>>> isinstance(bool, (bool, list[bool]))
TypeError: isinstance() argument 2 cannot be a parameterized generic

>>> issubclass(bool, (bool, list[bool]))
True

@OP, this seems like a bug. issubclass should raise according to PEP 585. In this case it's short-circuiting so it doesn't check the 2nd arg. Swapping the arguments around gives the expected TypeError.

>>> issubclass(bool, (list[bool], bool))
TypeError: issubclass() argument 2 cannot be a parameterized generic

At the same time, I'm hesitant to run a check through everything in the types tuple first. That would hurt issubclass performance (I'm not sure by how much). What do you think, Guido and Serhiy?

----------
components: +Library (Lib) -Tests, Windows
nosy: +gvanrossum, kj, serhiy.storchaka -paul.moore, steve.dower, tim.golden, zach.ware
versions: +Python 3.11, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44932>
_______________________________________


More information about the Python-bugs-list mailing list