sbdchd / django-types

:doughnut: Type stubs for Django
MIT License
188 stars 62 forks source link

Make get_user_model return Type[AbstractBaseUser] instead of Type[Model] #134

Closed monosans closed 1 year ago

sbdchd commented 1 year ago

Hmm what's the difference between Model and AbstractBaseUser?

monosans commented 1 year ago

Hmm what's the difference between Model and AbstractBaseUser?

Some functions/methods in django-types receive an argument of AbstractBaseUser type, and get_user_model returns Type[Model]. Here is a sample code which is actually correct, but we get a type error:

from django.contrib.auth import get_user_model, update_session_auth_hash
from django.http import HttpRequest, HttpResponse
from django.shortcuts import get_object_or_404

User = get_user_model()

def foo(request: HttpRequest, pk: int) -> HttpResponse:
    user = get_object_or_404(User, pk=pk)

    # Argument of type "Model" cannot be assigned to parameter "user" of type
    # "AbstractBaseUser" in function "update_session_auth_hash" "Model" is
    # incompatible with "AbstractBaseUser"
    update_session_auth_hash(request, user)

    ...