Search code examples
pythondjangovisual-studio-codepylance

How can I supress this type error from Django create_user?


I'm getting the following type error from pylance:

from django.contrib.auth.models import User, AbstractUser
from django.contrib.auth import get_user_model

get_user_model().objects.create_user(**user_data)
#                        ^- Cannot access member "create_user" for type "BaseManager[Any]"
#                           Member "create_user" is unknown Pylance reportGeneralTypeIssues

# User.objects.create_user(**user_data) # same error
# AbstractUser.objects.create_user(**user_data) # same error

For some reason it thinks AbstractUser.objects has a broader type BaseManager[Any] instead of UserManager, even though AbstractUser defines objects = UserManager().

The code works without errors when tested.

Does anyone know how I can supress or get rid of this sort of type error?


Solution

  • When you define objects in your User model you should give it a type like this:

    class User(AbstractBaseUser):
        ...
        
       objects: UserManager = UserManager()