django-stubs
django-stubs copied to clipboard
Error when overriding user manager
Bug report
I'm overwritting the user manager like:
class MyUserManager(BaseUserManager):
def create_user(
self,
facebook_email: str,
facebook_first_name: str,
facebook_last_name: str,
facebook_uid: str,
facebook_access_token: str,
**extra_fields
):
.....
class User(AbstractBaseUser):
(...)
objects = MyUserManager()
What's wrong
mypy returns:
accounts/models.py:31: error: Missing type parameters for generic type "BaseUserManager"
How is that should be
It should just work :)
System information
- OS: Ubuntu 18.04
pythonversion: 3.6djangoversion: 2.2mypyversion: 0.761django-stubsversion: 1.4.0
Couldn't/shouldn't this be resolved by doing the following?
class MyUserManager(BaseUserManager["User"]):
...
class User(AbstractBaseUser):
...
objects = MyUserManager()
If the PR linked to close this issue, #360 removing arguments, is merged, I don't think there's a possibility to properly type Manager.model for the user manager?
https://github.com/typeddjango/django-stubs/blob/8d8b8cd1fc8b54eac799905dbbffc4e21708b6da/django-stubs/db/models/manager.pyi#L34
Which in addition, if I'm overriding e.g. def create_user and use something like:
user = self.model(*args, **kwargs)
user.save()
return user # <- `user` is now `Any` as `self.model` can't be resolved..
I'm returning Any..
I was suffering the same issue, creating a custom manager which needed to be typed with the model class that hadn't been declared yet because the model required the manager and doing it like this worked so i think this isn't an issue:
class PublishedManager(models.Manager["Post"]):
def get_queryset(self) -> models.QuerySet["Post"]:
return super().get_queryset() .filter(status=1)