如何解决我如何告诉 pytype 实例是抽象基类的子类,而不是抽象基类本身?
当代码实例化超类型的实例时会出现问题。这是一个例子。
T = TypeVar("T")
class A(Generic[T],metaclass=abc.ABCMeta):
@abc.abstractmethod
def hello(self,arg: T):
raise NotImplementedError
class B(A[float]):
def hello(self,arg: float):
print("class B")
def make_an_a_subclass(clsname: Type[A]) -> A:
return clsname() # error on this line
b = make_an_a_subclass(B) b.hello(1.0)
pytype 返回一个错误,我无法实例化“clsname”,因为 clsname 是 A 的一个实例,它是抽象的:
不能用抽象方法实例化 A hello [not-instantiable]
但我希望 make_an_a_subclass 接受一个类型,它是 A 的任何子类,但不是 A 本身。有没有办法对此进行注释?
注意,这已在 mypy 中明确实现:https://github.com/python/mypy/pull/2853 似乎只会导致 pytype 出现问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。