From e6724d5d798cbe076be662c8fb080bf2fcf6bcc8 Mon Sep 17 00:00:00 2001 From: Alex Morega Date: Thu, 25 Jun 2026 15:49:58 +0300 Subject: [PATCH 1/2] Fix return type of the `for_user` method --- ninja_jwt/tokens.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ninja_jwt/tokens.py b/ninja_jwt/tokens.py index 18bd7e650..3e301c3bc 100644 --- a/ninja_jwt/tokens.py +++ b/ninja_jwt/tokens.py @@ -1,5 +1,5 @@ from datetime import datetime, timedelta -from typing import Any, Optional, Tuple +from typing import Any, Optional, Self, Tuple from uuid import uuid4 from django.conf import settings @@ -181,7 +181,7 @@ def check_exp(self, claim: str = "exp", current_time: Optional[datetime] = None) raise TokenError(format_lazy(_("Token '{}' claim has expired"), claim)) @classmethod - def for_user(cls, user: AbstractBaseUser) -> "Token": + def for_user(cls, user: AbstractBaseUser) -> Self: """ Returns an authorization token for the given user that will be provided after authenticating the user's credentials. @@ -253,7 +253,7 @@ def blacklist(self) -> BlacklistedToken: return BlacklistedToken.objects.get_or_create(token=token) @classmethod - def for_user(cls, user: "AbstractBaseUser") -> Token: + def for_user(cls, user: "AbstractBaseUser") -> Self: """ Adds this token to the outstanding token list. """ From d3be6cd7ca2a5554defdf3843c29d1135ed9fade Mon Sep 17 00:00:00 2001 From: Alex Morega Date: Thu, 25 Jun 2026 16:06:26 +0300 Subject: [PATCH 2/2] Use `TypeVar` instead of `Self` for py<3.11 --- ninja_jwt/tokens.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ninja_jwt/tokens.py b/ninja_jwt/tokens.py index 3e301c3bc..e673c6b9e 100644 --- a/ninja_jwt/tokens.py +++ b/ninja_jwt/tokens.py @@ -1,5 +1,5 @@ from datetime import datetime, timedelta -from typing import Any, Optional, Self, Tuple +from typing import Any, Optional, Tuple, TypeVar from uuid import uuid4 from django.conf import settings @@ -12,6 +12,8 @@ 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") + class Token: """ @@ -181,7 +183,7 @@ def check_exp(self, claim: str = "exp", current_time: Optional[datetime] = None) raise TokenError(format_lazy(_("Token '{}' claim has expired"), claim)) @classmethod - def for_user(cls, user: AbstractBaseUser) -> Self: + def for_user(cls: T, user: AbstractBaseUser) -> T: """ Returns an authorization token for the given user that will be provided after authenticating the user's credentials. @@ -253,7 +255,7 @@ def blacklist(self) -> BlacklistedToken: return BlacklistedToken.objects.get_or_create(token=token) @classmethod - def for_user(cls, user: "AbstractBaseUser") -> Self: + def for_user(cls: T, user: "AbstractBaseUser") -> T: """ Adds this token to the outstanding token list. """