Author serhiy.storchaka
Recipients gvanrossum, kj, serhiy.storchaka
Date 2021-07-18.17:21:30
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1626628890.53.0.612385746945.issue44668@roundup.psfhosted.org>
In-reply-to
Content
1. Checks for types.Union ignore any non-type args. Checks for typing.Union require fail on first checked non-type (but it can short circuit).

>>> import typing
>>> T = typing.TypeVar('T')
>>> issubclass(int, int | T | str)
True
>>> issubclass(int, str | T | int)
True
>>> issubclass(int, typing.Union[int, T, str])
True
>>> issubclass(int, typing.Union[str, T, int])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/serhiy/py/cpython/Lib/typing.py", line 1208, in __subclasscheck__
    if issubclass(cls, arg):
       ^^^^^^^^^^^^^^^^^^^^
TypeError: issubclass() arg 2 must be a class, a tuple of classes, or a union.
>>> isinstance(1, int | T | str)
True
>>> isinstance(1, str | T | int)
True
>>> isinstance(1, typing.Union[int, T, str])
True
>>> isinstance(1, typing.Union[str, T, int])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/serhiy/py/cpython/Lib/typing.py", line 1204, in __instancecheck__
    return self.__subclasscheck__(type(obj))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/typing.py", line 1208, in __subclasscheck__
    if issubclass(cls, arg):
       ^^^^^^^^^^^^^^^^^^^^
TypeError: issubclass() arg 2 must be a class, a tuple of classes, or a union.

2. __instancecheck__ of typing.Union uses __subclasscheck__ of args instead of __instancecheck__. In normal cases the result should be the same, but there should be a reason of having two different special methods.
History
Date User Action Args
2021-07-18 17:21:30serhiy.storchakasetrecipients: + serhiy.storchaka, gvanrossum, kj
2021-07-18 17:21:30serhiy.storchakasetmessageid: <1626628890.53.0.612385746945.issue44668@roundup.psfhosted.org>
2021-07-18 17:21:30serhiy.storchakalinkissue44668 messages
2021-07-18 17:21:30serhiy.storchakacreate