我如何告诉 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 (将#修改为@)