Subclass in type hinting
When you specify cls: A
, you're saying that cls
expects an instance of type A
. The type hint to specify cls
as a class object for the type A
(or its subtypes) uses typing.Type
.
from typing import Typedef process_any_subclass_type_of_A(cls: Type[A]): pass
From The type of class objects:
Sometimes you want to talk about class objects that inherit from agiven class. This can be spelled as
Type[C]
whereC
is a class. Inother words, whenC
is the name of a class, usingC
to annotate anargument declares that the argument is an instance ofC
(or of asubclass ofC
), but usingType[C]
as an argument annotation declaresthat the argument is a class object deriving fromC
(orC
itself).