Instances of types.GenericAlias are accepted as the first argument of issubclass():
>>> issubclass(list[int], object)
True
>>> issubclass(list[int], type)
False
while instances of typing.GenericAlias are not:
>>> issubclass(typing.List[int], object)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: issubclass() arg 1 must be a class
>>> issubclass(typing.List[int], type)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: issubclass() arg 1 must be a class
Although both are rejected if the second argument is an abstract class:
>>> issubclass(list[int], collections.abc.Sequence)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/serhiy/py/cpython/Lib/abc.py", line 123, in __subclasscheck__
return _abc_subclasscheck(cls, subclass)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: issubclass() arg 1 must be a class
>>> issubclass(typing.List[int], collections.abc.Sequence)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/serhiy/py/cpython/Lib/abc.py", line 123, in __subclasscheck__
return _abc_subclasscheck(cls, subclass)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: issubclass() arg 1 must be a class
Usually issubclass(x, y) is preceded by the check isinstance(x, type), so the final result will always be false since 3.11, but if that check is omitted, you can see a difference.
Linked PRs
Instances of
types.GenericAliasare accepted as the first argument ofissubclass():while instances of
typing.GenericAliasare not:Although both are rejected if the second argument is an abstract class:
Usually
issubclass(x, y)is preceded by the checkisinstance(x, type), so the final result will always be false since 3.11, but if that check is omitted, you can see a difference.Linked PRs
issubclasswithGenericAliasas the 1st arg #103369