-
-
Notifications
You must be signed in to change notification settings - Fork 515
Treat get_user_model as a dynamic class factory #1730
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
Changes from 1 commit
e9529fa
6d493d8
990fb64
05c09f5
42c8eee
f5ed69d
10d3528
8f72a8b
fdea347
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,12 +7,12 @@ from django.db.models.base import Model | |
| from django.http.request import HttpRequest | ||
| from typing_extensions import TypeAlias | ||
|
|
||
| UserModel = get_user_model() | ||
| _AnyUser: TypeAlias = UserModel | AnonymousUser | ||
| _UserModel = get_user_model() | ||
|
||
| _AnyUser: TypeAlias = _UserModel | AnonymousUser | ||
|
|
||
| class BaseBackend: | ||
| def authenticate(self, request: HttpRequest | None, **kwargs: Any) -> UserModel | None: ... | ||
| def get_user(self, user_id: int) -> UserModel | None: ... | ||
| def authenticate(self, request: HttpRequest | None, **kwargs: Any) -> _UserModel | None: ... | ||
| def get_user(self, user_id: int) -> _UserModel | None: ... | ||
| def get_user_permissions(self, user_obj: _AnyUser, obj: Model | None = ...) -> set[str]: ... | ||
| def get_group_permissions(self, user_obj: _AnyUser, obj: Model | None = ...) -> set[str]: ... | ||
| def get_all_permissions(self, user_obj: _AnyUser, obj: Model | None = ...) -> set[str]: ... | ||
|
|
@@ -21,7 +21,7 @@ class BaseBackend: | |
| class ModelBackend(BaseBackend): | ||
| def authenticate( | ||
| self, request: HttpRequest | None, username: str | None = ..., password: str | None = ..., **kwargs: Any | ||
| ) -> UserModel | None: ... | ||
| ) -> _UserModel | None: ... | ||
| def has_module_perms(self, user_obj: _AnyUser, app_label: str) -> bool: ... | ||
| def user_can_authenticate(self, user: _AnyUser | None) -> bool: ... | ||
| def with_perm( | ||
|
|
@@ -30,11 +30,11 @@ class ModelBackend(BaseBackend): | |
| is_active: bool = ..., | ||
| include_superusers: bool = ..., | ||
| obj: Model | None = ..., | ||
| ) -> QuerySet[UserModel]: ... | ||
| ) -> QuerySet[_UserModel]: ... | ||
|
|
||
| class AllowAllUsersModelBackend(ModelBackend): ... | ||
|
|
||
| _U = TypeVar("_U", bound=UserModel) | ||
| _U = TypeVar("_U", bound=_UserModel) | ||
|
|
||
| class RemoteUserBackend(ModelBackend): | ||
| create_unknown_user: bool | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ from django.http.response import HttpResponse, HttpResponseRedirect | |
| from django.views.generic.base import TemplateView | ||
| from django.views.generic.edit import FormView | ||
|
|
||
| UserModel = get_user_model() | ||
|
||
| _UserModel = get_user_model() | ||
|
|
||
| class RedirectURLMixin: | ||
| next_page: str | None | ||
|
|
@@ -65,7 +65,7 @@ class PasswordResetConfirmView(PasswordContextMixin, FormView): | |
| token_generator: Any | ||
| validlink: bool | ||
| user: Any | ||
| def get_user(self, uidb64: str) -> UserModel | None: ... | ||
| def get_user(self, uidb64: str) -> _UserModel | None: ... | ||
|
|
||
| class PasswordResetCompleteView(PasswordContextMixin, TemplateView): | ||
| title: Any | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get stubtest errors if I leave this as it. Not entirely sure why. See the CI failures here: https://github.com/typeddjango/django-stubs/actions/runs/6267951533/job/17022010362
This type does exist at runtime though, but I'm not sure if we should expose it or not? Pretty sure it's not intended to be public API 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it exist at runtime we can start out by exposing it, regardless of public API or not I'd say
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It exists at runtime, but the types seem to be different, so subtest complains. Is there a way to ignore things from stubtest? Is that what the allowlist is for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes precisely. Update the allowlist, that's what it's for
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But before we do, can you inspect what the runtime type is?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what subtest says at least:
Seems to be the same for all the attributes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aha, interesting, that's a descriptor: https://github.com/django/django/blob/4de31ec680df062e5964b630f1b881ead5004e15/django/db/models/query_utils.py#L178
Should be fine to type as "what it really is"