Annotations

class extended_mypy_django_plugin.Concrete

The Concrete annotation exists as a class with functionality for both runtime and static type checking time.

At static type checking time (specifically with mypy) it is used to create a type that represents the Union of all the concrete children of some abstract model.

classmethod cast_as_concrete(obj: T_Obj) T_Obj

This can be used to change the type of an abstract django model to be only a concrete decedent.

At runtime this will raise an exception if the object is an abstract model or class.

At static type checking time this will change the type of the variable being assigned to:

from typing import Self, cast
from extended_mypy_django_plugin import Concrete

class MyAbstractModel(Model):
    class Meta:
        abstract = True

    @classmethod
    def new(cls) -> Self:
        cls = Concrete.cast_as_concrete(cls)
        reveal_type(cls) # type[Concrete1] | type[Concrete2] | type[Concrete3] | ...
        ...

    def get_self(self) -> Self:
        self = Concrete.cast_as_concrete(self)
        reveal_type(self) # Concrete1 | Concrete2 | Concrete3 | ...
        ...

This can also be used outside of a model method:

model: type[MyAbstractModel] = Concrete1
narrowed = Concrete.cast_as_concrete(model)
reveal_type(narrowed) # Concrete1 | Concrete2 | Concrete3 | ...
class extended_mypy_django_plugin.DefaultQuerySet

This is used to annotate a model such that the mypy plugin may turn this into a union of all the default querysets for all the concrete children of the specified abstract model class, or of that model when it is a concrete model