How do I tell pytype that an instance is a subclass of an abstract base, but not the abstract base itself?

Viewed 244

The issue arises when code instantiates an instance of the supertype. Here's an example.

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 returns an error, that I can't instantiate "clsname" because clsname is an instance of A, which is abstract:

Can't instantiate A with abstract methods hello [not-instantiable]

But I want make_an_a_subclass to accept a Type that is any subclass of A, but not A itself. Is there a way to annotate this?

Note, this has been made explicitly possible in mypy: https://github.com/python/mypy/pull/2853 Only seems to cause problems for pytype.

0 Answers
Related