Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

user_passes_test should take in AUTH_USER_MODEL #1058

Open
ljodal opened this issue Jul 13, 2022 · 6 comments · May be fixed by #1229 or #1730
Open

user_passes_test should take in AUTH_USER_MODEL #1058

ljodal opened this issue Jul 13, 2022 · 6 comments · May be fixed by #1229 or #1730

Comments

@ljodal
Copy link
Contributor

ljodal commented Jul 13, 2022

Starting with #1038 user_passes_test is typed to accept AbstractBaseUser | AnonymousUser. While technically correct it would be great if the mypy plugin could update this to be the actual model defined in settings. For example we have code like this that now complains:

@user_passes_test(lambda u: u.is_superuser)
def my_view():
    ...
@ljodal ljodal changed the title `user_passes_tes user_passes_test should take in AUTH_USER_MODEL Jul 13, 2022
@sterliakov
Copy link
Contributor

Nice idea. One thing to note: it should replace with AUTH_USER_MODEL | AnonymousUser, not with authenticated user only.

@PIG208
Copy link
Contributor

PIG208 commented Aug 8, 2022

Yes, I'm working on a fix for this.

Edit: it might be actually more complicated, as I'm not sure how the mypy api allows overriding the type information of a decorator.

@PIG208
Copy link
Contributor

PIG208 commented Sep 24, 2022

A way to solve this might be changing the way we handle AUTH_USER_MODEL. We can create a stub-only dummy type _AuthUser, and use get_type_analyze_hook() to replace this type with the proper model or fall back to AbstractBaseUser. Then we can use this dummy type in the stub wherever an authenticated user is expected and also remove the old set_auth_user_model_as_type_for_request_user thing.

@dimbleby
Copy link

dimbleby commented Nov 3, 2022

For example we have code like this that now complains ...

with the release of 1.13.0, I imagine more people are likely to find this issue. Is there a sensible way to annotate my code so that django-stubs does not complain?

ljodal added a commit to ljodal/django-stubs that referenced this issue Nov 3, 2022
Add a transformer that updates the signature of user_passes_test to
reflect settings.AUTH_USER_MODEL.

Fixes typeddjango#1058
ljodal added a commit to ljodal/django-stubs that referenced this issue Nov 3, 2022
Add a transformer that updates the signature of user_passes_test to
reflect settings.AUTH_USER_MODEL.

Fixes typeddjango#1058
@ljodal ljodal linked a pull request Nov 3, 2022 that will close this issue
@Archmonger
Copy link

@PIG208 Are you willing to PR your suggested behavior?

@ljodal ljodal linked a pull request Sep 21, 2023 that will close this issue
@spapas
Copy link

spapas commented Feb 2, 2024

I've also stumbled upon this... I've got this decorator:

def any_permission_required(*args):
    return user_passes_test(lambda u: any(u.has_perm(perm) for perm in args))

and when I try to run mypy I get:

users\urls.py:8: error: Item "AbstractBaseUser" of "AbstractBaseUser | AnonymousUser" has no attribute "has_perm"  [union-attr]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
6 participants