与 Python 3.5 中的 typing.Generic 相比,issubclass 是否损坏?

Is issubclass broken for comparing with typing.Generic in Python 3.5?

如何确定给定的 class 是否是任何 typing.Generic 的子class?调用 issubclass 似乎没有像我预期的那样工作:

import typing

T = typing.TypeVar('T')

class A(typing.Generic[T]):
    pass

class B:
    pass

issubclass(A, typing.Generic)
>>> True

issubclass(B, typing.Generic)
>>> Traceback (most recent call last):
... <more traceback lines here>
File "<some_path>\env\lib\abc.py", line 225, in __subclasscheck__
    for scls in cls.__subclasses__():
TypeError: descriptor '__subclasses__' of 'type' object needs an argument

此外,我是否遗漏了什么,这是预期的行为还是错误?

那么,如果 TypeError 被引发,那么它不是 typing.Generic 的子 class 是否安全?

我正在使用 Python 3.5.2.

谢谢。

这是预期的行为(至少目前如此)。您不能将类型提示上下文中指定的类型与 class 混合使用。您可以将 type 视为标记,指示可能的类型检查器。 class 是您与之交互的运行时对象。

可以在下面找到更多信息 thread on the issue tracker for the typing module (specifically, look in this post).

郑重声明,typing 仍然是 临时的 ,在它被认为稳定之前,可能会出现许多错误和许多更改,他们仍在对其进行调整,无法保证到位。例如,在 Python 3.5.1 中,您的代码运行顺畅 w/o 任何 TypeError s(根据我的收集,它们是在 3.5.2 中引入的):

issubclass(A, typing.Generic)
Out[28]: True

issubclass(B, typing.Generic)
Out[31]: False