Skip to content

request.user does not use custom User model #515

Open
@ollejernstrom

Description

@ollejernstrom

Bug report

What's wrong

These are from the Request object in the stubbs.

def user(self) -> AbstractBaseUser | AnonymousUser: ...
@user.setter
def user(self, value: AbstractBaseUser | AnonymousUser) -> None: ...

However i have a different auth model defined in my settings.py called CustomUser

AUTH_USER_MODEL = "base.CustomUser"

Is there a way to get this typing correct.

Use case error:

user = self.request.user
if user.can_manage():
    return queryset
products/views.py:145: error: Item "AnonymousUser" of "AbstractBaseUser | AnonymousUser" has no attribute "can_manage"  [union-attr]

Furthermore, if the permission classes dont allow "AnonymousUser" can we type narrow that somehow? (Like in django-stubs

How can I create a HttpRequest that's guaranteed to have an authenticated user?

How is that should be

It should be narrowed to just my CustomUser

TL;DR:

  • How can i only see CustomUser instead of AbstractBaseUser | AnonymousUser

System information

  • OS: MAC
  • python version: 3.11
  • django version: 4.2
  • mypy version: 1.7.1
  • django-stubs version: (latest)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions