New submission from Serhiy Storchaka <[email protected]>:
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.
----------
messages: 397757
nosy: gvanrossum, kj, serhiy.storchaka
priority: normal
severity: normal
status: open
title: More differences in instance and subclass checks between typing.Union
and types.Union
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue44668>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com