Python type hinting: how to tell X is a subclass for Foo?
Solution 1
It seems like other current (22 Sep 2016) answers here are incorrect. According to PEP 484 (about Type Hints), there exists a hint for type of class objects, called Type[C]. And according to typing
module's documentation, you can use typing.Type[C] to achieve exactly what you want. I'm using those myself with Python 3.5.2.
Quoting the PEP:
Sometimes you want to talk about class objects, in particular class objects that inherit from a given class. This can be spelled as Type[C] where C is a class. To clarify: while C (when used as an annotation) refers to instances of class C , Type[C] refers to subclasses of C .
And quoting the docs:
A variable annotated with C may accept a value of type C. In contrast, a variable annotated with Type[C] may accept values that are classes themselves – specifically, it will accept the class object of C.
And referring to your specific example:
import typing
class A(object):
pass
class B(A):
pass
def register(cls: typing.Type[A]):
assert issubclass(cls, A)
register(A)
register(B)
You can check such code statically using mypy, and it should work in simple cases -- beware however that mypy is a work in progress, as of now there are several issues open about Type[C] hinting.
Solution 2
To solve your general case, you would have to write a metaclass with a suitable __subclasscheck__
. Possible, but cumbersome.
In your specific case of Django model classes, an explicit metaclass already exists, so annotating that should do the job:
import django.db.model as model
def register(cls: model.base.ModelBase): ...
This will work because isinstance(models.Model, models.base.ModelBase)
is true.
vdboor
Software/web developer with enthusiasm for technology and the Open Source world. Driven to create the best out of technology, solving things in a proper way, and creating solutions with tangible benefits to the customer.
Updated on June 22, 2022Comments
-
vdboor about 2 years
How should I write a type hint for class types in Python? Consider this code:
class A(object): pass class B(A): pass def register(cls: type[A]): assert issubclass(cls, A) register(A) register(B)
Is
type[A]
the correct way to write this? If I'd just usecls: A
it would meancls
is an instance ofA
, but I want to to saycls
is a class/type, which at least subclassesA
.Specifically, what I want to indicate is that the parameter should be a Django model type.
-
laike9m almost 5 yearsWhat about multiple inheritance, is it possible to annotate cls must belong to two types?
-
mbdevpl almost 5 years@laike9m As of today, no: github.com/python/typing/issues/213 - but it might land in Python one day.