Python type hinting: how to tell X is a subclass for Foo?
Question: 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 use `cls: A` it would
mean `cls` is an instance of `A`, but I want to to say `cls` is a class/type,
which at least subclasses `A`.

Specifically, what I want to indicate is that the parameter should be a Django
model type.


Answer: 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.

