Content-Length: 304898 | pFad | https://github.com/python/cpython/issues/101162

ad issubclass() is inconsistent with generic aliases · Issue #101162 · python/cpython · GitHub
Skip to content

issubclass() is inconsistent with generic aliases #101162

Description

@serhiy-storchaka

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions









    ApplySandwichStrip

    pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


    --- a PPN by Garber Painting Akron. With Image Size Reduction included!

    Fetched URL: https://github.com/python/cpython/issues/101162

    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy