Skip to content

Fix return type of the for_user method#179

Open
mgax wants to merge 2 commits into
eadwinCode:masterfrom
mgax:fix/for-user-return-type
Open

Fix return type of the for_user method#179
mgax wants to merge 2 commits into
eadwinCode:masterfrom
mgax:fix/for-user-return-type

Conversation

@mgax

@mgax mgax commented Jun 25, 2026

Copy link
Copy Markdown

The for_user classmethod returns an instance of cls(), but it claims to return Token:

@classmethod
def for_user(cls, user: AbstractBaseUser) -> "Token":

To get the access token from RefreshToken.for_user, one has to cast the return value in order to make the type checker happy:

refresh_token = cast(RefreshToken, RefreshToken.for_user(user))
access_token = refresh_token.access_token

This PR removes the need for that cast by annotating for_user to return Self, which may be Token, or the subclass.

Comment thread ninja_jwt/tokens.py
from .token_blacklist.models import BlacklistedToken, OutstandingToken
from .utils import aware_utcnow, datetime_from_epoch, datetime_to_epoch, format_lazy

T = TypeVar("T", bound="Token")

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I tried to use typing.Self at first but it was only introduced in Python 3.11 and the testsuite checks against older Pythons.

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.

1 participant