-
-
Notifications
You must be signed in to change notification settings - Fork 474
create and re-use TypeAlias
es and TypeVar
s for "user" and "any user"
#2384
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
create and re-use TypeAlias
es and TypeVar
s for "user" and "any user"
#2384
Conversation
08131cc
to
513df00
Compare
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.
Generally - looks great! Just one note
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 don't think we should change the base class to be AbstractUser
. This is a section of what the "Specifying a custom user model" says in the docs:
[...] The easiest way to construct a compliant custom user model is to inherit from AbstractBaseUser. AbstractBaseUser provides the core implementation of a user model, including hashed passwords and tokenized password resets. [...]
https://docs.djangoproject.com/en/5.1/topics/auth/customizing/#specifying-a-custom-user-model
As far as I understand, this is not the base class, this is the default type, unless |
That's interesting that they suggest that... The issue is that It does look like there's probably some complications if we move the base class up (subclasses of |
513df00
to
06a4f41
Compare
I rolled back the change since it does affect a user who subclasses This change still adds some helpful aliases so it can get merged in and the follow-up can be done independently. |
AUTH_USER_MODEL
to be AbstractUser
and reuse aliasesTypeAlias
es and TypeVar
s for User and AnyUser
TypeAlias
es and TypeVar
s for User and AnyUserTypeAlias
es and TypeVar
s for "user" and "any user"
bf15c5b
to
2697324
Compare
This change also adds a few user type vars and alises to cover the common use cases of ``User``, ``User | AnonymousUser``, and their ``TypeVar`` forms for using in generic contexts.
2697324
to
e29f60b
Compare
@sobolevn any chance I can get a re-review? I updated this to fix the conflicts, but it was already updated to remove the concern about shifting the base user. It now only provides a few useful type aliases that can be shared across the codebase. High level changes:
|
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.
Thanks for the ping! Mostly looks good.
def get_user_model() -> type[_UserModel]: ... | ||
def get_user(request: HttpRequest | Client) -> _UserModel | AnonymousUser: ... | ||
async def aget_user(request: HttpRequest | Client) -> _UserModel | AnonymousUser: ... | ||
def get_user_model() -> type[_User]: ... |
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.
def get_user_model() -> type[_User]: ... | |
def get_user_model() -> UserModel: ... |
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'll look to see if there are any type[_User]
that can be replaced with the UserModel
alias
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 think there was just one other that you didn't mention
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.
The suggestion seems reasonable, but a few comments and I just want to understand what you'd prefer before continuing.
def get_user_model() -> type[_UserModel]: ... | ||
def get_user(request: HttpRequest | Client) -> _UserModel | AnonymousUser: ... | ||
async def aget_user(request: HttpRequest | Client) -> _UserModel | AnonymousUser: ... | ||
def get_user_model() -> type[_User]: ... |
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'll look to see if there are any type[_User]
that can be replaced with the UserModel
alias
Yes, I prefer re-exporting over re-definition, because definition can change at some point :) |
(missclick, sorry) |
Ruff really doesn't like re-exporting with a rename, but I think I finally got it to stop deleting the re-exports 😒 |
202711d
to
fb5379a
Compare
updated |
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.
Thanks!
I have made things!
This change also adds a few user type vars and alises to cover the common use cases of
User
,User | AnonymousUser
, and theirTypeVar
forms for using in generic contexts.