Skip to content

Improve type of Token.for_user to allow subclasses#776

Merged
50-Course merged 1 commit intojazzband:masterfrom
sterliakov:master
Feb 2, 2024
Merged

Improve type of Token.for_user to allow subclasses#776
50-Course merged 1 commit intojazzband:masterfrom
sterliakov:master

Conversation

@sterliakov
Copy link
Copy Markdown
Contributor

The same approach is already used in BlacklistMixin. The issue with existing Token return type can be demonstrated by

from django.contrib.auth.models import User
from rest_framework_simplejwt.tokens import RefreshToken

user: User
token = RefreshToken.for_user(user)
token.access_token  # E: "Token" has no attribute "access_token"  [attr-defined]


@classmethod
def for_user(cls, user: AuthUser) -> "Token":
def for_user(cls: Type[T], user: AuthUser) -> T:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @sterliakov,
Thank you for this patch. This approach demonstrate a forward-thinking approach. In this case, modifying the "Token" user to generic type [T] address the issue in a clear concise way since, a similar pattern had already appeared in the codebase.

While there is no breaking changes at the moment, would you be interested to write tests for this functionality focusing on its impact on the Token class itself and the subclass, RefreshToken?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! I'd be interested in contributing to type checking of this project in Jan.

There are other typing issues I spotted (e.g. TokenObtainSerializer should be generic in token type to have correct get_token return type), and I have a typed codebase (not OSS, unfortunately) where many components of this package are extended (so some issues will pop up when I upgrade).

Regarding the tests - it would be not as straightforward, unfortunately, due to django-stubs complicated nature that requires code import and execution. Some aspects can be tested trivially, though, and I am willing to cover such patterns. Typechecking tests are usually of shape "here's the file, run mypy on it and check output" (you can find examples e.g. here).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ough. I failed to notice this initially, but I also want to add mypy to CI very much: types seem to be not checked at all now.

Copy link
Copy Markdown
Member

@50-Course 50-Course Dec 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would re-review after the holidays! Happy new year in advance @sterliakov 🌟

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @sterliakov, reopening discussion on this patch, we could work with mypy on a separate MR

Copy link
Copy Markdown
Member

@50-Course 50-Course left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants