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: `issubclass` and `isinstance` doesn't check for all 2nd argument types
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: Crowthebird, gvanrossum, kj, serhiy.storchaka
Priority: normal Keywords:

Created on 2021-08-17 07:26 by Crowthebird, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg399717 - (view) Author: Jeremiah Gabriel Pascual (Crowthebird) * Date: 2021-08-17 07:26
When I tried using `isinstance` with a type (e.g. `bool`) as the 1st argument and a parameterized generic in a tuple (e.g. '(`bool`, `list[bool]`)') as the 2nd argument, it raised a `TypeError`, 'isinstance() argument 2 cannot be a parameterized generic'. But when I did the same thing in `issubclass`, it returned a boolean and did not raise any `TypeError`s. Using `isinstance` with an object as the 1st argument and the same tuple 2nd argument also returned a boolean, and did not raise any `TypeError`s. Is this expected behaviour, or should this be fixed? This was tested in Python 3.10.0rc1 in a 64-bit system.
msg399722 - (view) Author: Ken Jin (kj) * (Python committer) Date: 2021-08-17 09:31
@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?
msg399741 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-08-17 11:17
It is not specific to typing.

>>> isinstance(1, (int, 1))
True
>>> isinstance(1, (1, int))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: isinstance() arg 2 must be a type or tuple of types
>>> issubclass(int, (int, 1))
True
>>> issubclass(int, (1, int))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: issubclass() arg 2 must be a class or tuple of classes

isinstance() and issubclass() with tuples always stopped on first positive match. I don't think there is need to change this.
msg399745 - (view) Author: Ken Jin (kj) * (Python committer) Date: 2021-08-17 11:57
Thank you for the examples Serhiy. I agree.
msg399783 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-08-17 19:09
Agreed. Thanks, Serhiy.
History
Date User Action Args
2022-04-11 14:59:48adminsetgithub: 89095
2021-08-17 19:09:23gvanrossumsetmessages: + msg399783
2021-08-17 11:57:52kjsetstatus: open -> closed
resolution: wont fix
messages: + msg399745

stage: resolved
2021-08-17 11:17:31serhiy.storchakasetmessages: + msg399741
2021-08-17 09:31:53kjsetversions: + Python 3.9, Python 3.11
nosy: + gvanrossum, serhiy.storchaka, kj, - paul.moore, tim.golden, zach.ware, steve.dower

messages: + msg399722

components: + Library (Lib), - Tests, Windows
2021-08-17 07:26:02Crowthebirdcreate