python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.57k stars 2.85k forks source link

Question: How to bound a mixin to a subclass #8705

Open canassa opened 4 years ago

canassa commented 4 years ago

I created a mixin for Django models and I would like to know how to properly type it, e.g.:

from django.db import models

T = TypeVar("T", bound=models.Model)

class SafeCreateMixin:
    @classmethod
    @transaction.atomic()
    def safe_create(cls: Type[T], **kwargs) -> Optional[T]:
        try:
            return cls.objects.create(**kwargs)
        except IntegrityError as e:
            return None

class MyModel(SafeCreateMixin, models.Model):
    ....

It fails with the following error:

error: The erased type of self "Type[django.db.models.base.Model]" is not a supertype of its class "Type[SafeCreateMixin]"

I understood that the mypy docs recommend creating a Protocol for mixins, but in my case, I just want to enforce that my mixins are added to a subclass of models.Model

erictraut commented 1 year ago

There isn't a way to declare this relationship in the type system today, but you can use a runtime assert to enforce it.

class SafeCreateMixin:
    @classmethod
    def safe_create(cls, **kwargs) -> Self | None:
        assert issubclass(cls, models.Model)
        try:
            return cls.objects.create(**kwargs)
        except IntegrityError:
            return None