You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
issubclass() is inconsistent with generic aliases #101162
>>> 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.
Moreover, all typing/types/genericalias tests pass with this change.
I think that it is a reasonable thing to do. Because GenericAlias already has __mro_entries__ method and exposing __bases__ does not actually solve any problems as far as I know.
Activity
sobolevn commentedon Apr 8, 2023
This happens because
PyObject_IsSubclassusesabstract_get_baseswhich returns(object,)forlist[int], but raises an error fortyping.List[int]:But, we can easily change this and forbid
__bases__access fortypes.GenericAliasobjects by adding it toattr_exceptions.As the result, everything is going to work the same as
typing._GenericAlias:Moreover, all typing/types/genericalias tests pass with this change.
I think that it is a reasonable thing to do. Because
GenericAliasalready has__mro_entries__method and exposing__bases__does not actually solve any problems as far as I know.pythongh-101162: Forbid using `issubclass` with `GenericAlias` as the…
gh-101162: Forbid using issubclass() with GenericAlias as the 1st arg (…
fix: use the origin type to when validating a grammar list
fix: use the origin type to when validating a grammar list
fix: use the origin type when validating a grammar list (#82)