Skip to content
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

Move Pyupgrade to python version 3.9 #854

Merged
merged 4 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ repos:
rev: 'v3.15.0'
hooks:
- id: pyupgrade
args: ['--py38-plus', '--keep-mock']
args: ['--py39-plus', '--keep-mock']

- repo: https://github.com/Lucas-C/pre-commit-hooks-markup
rev: v1.0.1
Expand Down
6 changes: 3 additions & 3 deletions rest_framework_simplejwt/authentication.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, Set, Tuple, TypeVar
from typing import Optional, TypeVar

from django.contrib.auth import get_user_model
from django.contrib.auth.models import AbstractBaseUser
Expand All @@ -17,7 +17,7 @@
if not isinstance(api_settings.AUTH_HEADER_TYPES, (list, tuple)):
AUTH_HEADER_TYPES = (AUTH_HEADER_TYPES,)

AUTH_HEADER_TYPE_BYTES: Set[bytes] = {
AUTH_HEADER_TYPE_BYTES: set[bytes] = {
h.encode(HTTP_HEADER_ENCODING) for h in AUTH_HEADER_TYPES
}

Expand All @@ -37,7 +37,7 @@ def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.user_model = get_user_model()

def authenticate(self, request: Request) -> Optional[Tuple[AuthUser, Token]]:
def authenticate(self, request: Request) -> Optional[tuple[AuthUser, Token]]:
header = self.get_header(request)
if header is None:
return None
Expand Down
8 changes: 4 additions & 4 deletions rest_framework_simplejwt/backends.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
from collections.abc import Iterable
from datetime import timedelta
from typing import Any, Dict, Optional, Type, Union
from typing import Any, Optional, Union

import jwt
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -46,7 +46,7 @@ def __init__(
issuer: Optional[str] = None,
jwk_url: Optional[str] = None,
leeway: Union[float, int, timedelta, None] = None,
json_encoder: Optional[Type[json.JSONEncoder]] = None,
json_encoder: Optional[type[json.JSONEncoder]] = None,
) -> None:
self._validate_algorithm(algorithm)

Expand Down Expand Up @@ -110,7 +110,7 @@ def get_verifying_key(self, token: Token) -> Optional[str]:

return self.verifying_key

def encode(self, payload: Dict[str, Any]) -> str:
def encode(self, payload: dict[str, Any]) -> str:
"""
Returns an encoded token for the given payload dictionary.
"""
Expand All @@ -132,7 +132,7 @@ def encode(self, payload: Dict[str, Any]) -> str:
# For PyJWT >= 2.0.0a1
return token

def decode(self, token: Token, verify: bool = True) -> Dict[str, Any]:
def decode(self, token: Token, verify: bool = True) -> dict[str, Any]:
"""
Performs a validation of the given token and returns its payload
dictionary.
Expand Down
4 changes: 2 additions & 2 deletions rest_framework_simplejwt/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, Optional, Union
from typing import Any, Optional, Union

from django.utils.translation import gettext_lazy as _
from rest_framework import exceptions, status
Expand Down Expand Up @@ -26,7 +26,7 @@ class DetailDictMixin:

def __init__(
self,
detail: Union[Dict[str, Any], str, None] = None,
detail: Union[dict[str, Any], str, None] = None,
code: Optional[str] = None,
) -> None:
"""
Expand Down
4 changes: 2 additions & 2 deletions rest_framework_simplejwt/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, Any, List, Optional, Union
from typing import TYPE_CHECKING, Any, Optional, Union

from django.contrib.auth import models as auth_models
from django.db.models.manager import EmptyManager
Expand Down Expand Up @@ -93,7 +93,7 @@ def get_all_permissions(self, obj: Optional[object] = None) -> set:
def has_perm(self, perm: str, obj: Optional[object] = None) -> bool:
return False

def has_perms(self, perm_list: List[str], obj: Optional[object] = None) -> bool:
def has_perms(self, perm_list: list[str], obj: Optional[object] = None) -> bool:
return False

def has_module_perms(self, module: str) -> bool:
Expand Down
18 changes: 9 additions & 9 deletions rest_framework_simplejwt/serializers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, Optional, Type, TypeVar
from typing import Any, Optional, TypeVar

from django.conf import settings
from django.contrib.auth import authenticate, get_user_model
Expand Down Expand Up @@ -29,7 +29,7 @@ def __init__(self, *args, **kwargs) -> None:

class TokenObtainSerializer(serializers.Serializer):
username_field = get_user_model().USERNAME_FIELD
token_class: Optional[Type[Token]] = None
token_class: Optional[type[Token]] = None

default_error_messages = {
"no_active_account": _("No active account found with the given credentials")
Expand All @@ -41,7 +41,7 @@ def __init__(self, *args, **kwargs) -> None:
self.fields[self.username_field] = serializers.CharField(write_only=True)
self.fields["password"] = PasswordField()

def validate(self, attrs: Dict[str, Any]) -> Dict[Any, Any]:
def validate(self, attrs: dict[str, Any]) -> dict[Any, Any]:
authenticate_kwargs = {
self.username_field: attrs[self.username_field],
"password": attrs["password"],
Expand Down Expand Up @@ -69,7 +69,7 @@ def get_token(cls, user: AuthUser) -> Token:
class TokenObtainPairSerializer(TokenObtainSerializer):
token_class = RefreshToken

def validate(self, attrs: Dict[str, Any]) -> Dict[str, str]:
def validate(self, attrs: dict[str, Any]) -> dict[str, str]:
data = super().validate(attrs)

refresh = self.get_token(self.user)
Expand All @@ -86,7 +86,7 @@ def validate(self, attrs: Dict[str, Any]) -> Dict[str, str]:
class TokenObtainSlidingSerializer(TokenObtainSerializer):
token_class = SlidingToken

def validate(self, attrs: Dict[str, Any]) -> Dict[str, str]:
def validate(self, attrs: dict[str, Any]) -> dict[str, str]:
data = super().validate(attrs)

token = self.get_token(self.user)
Expand All @@ -108,7 +108,7 @@ class TokenRefreshSerializer(serializers.Serializer):
"no_active_account": _("No active account found for the given token.")
}

def validate(self, attrs: Dict[str, Any]) -> Dict[str, str]:
def validate(self, attrs: dict[str, Any]) -> dict[str, str]:
refresh = self.token_class(attrs["refresh"])

user_id = refresh.payload.get(api_settings.USER_ID_CLAIM, None)
Expand Down Expand Up @@ -148,7 +148,7 @@ class TokenRefreshSlidingSerializer(serializers.Serializer):
token = serializers.CharField()
token_class = SlidingToken

def validate(self, attrs: Dict[str, Any]) -> Dict[str, str]:
def validate(self, attrs: dict[str, Any]) -> dict[str, str]:
token = self.token_class(attrs["token"])

# Check that the timestamp in the "refresh_exp" claim has not
Expand All @@ -165,7 +165,7 @@ def validate(self, attrs: Dict[str, Any]) -> Dict[str, str]:
class TokenVerifySerializer(serializers.Serializer):
token = serializers.CharField(write_only=True)

def validate(self, attrs: Dict[str, None]) -> Dict[Any, Any]:
def validate(self, attrs: dict[str, None]) -> dict[Any, Any]:
token = UntypedToken(attrs["token"])

if (
Expand All @@ -183,7 +183,7 @@ class TokenBlacklistSerializer(serializers.Serializer):
refresh = serializers.CharField(write_only=True)
token_class = RefreshToken

def validate(self, attrs: Dict[str, Any]) -> Dict[Any, Any]:
def validate(self, attrs: dict[str, Any]) -> dict[Any, Any]:
refresh = self.token_class(attrs["refresh"])
try:
refresh.blacklist()
Expand Down
4 changes: 2 additions & 2 deletions rest_framework_simplejwt/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import timedelta
from typing import Any, Dict
from typing import Any

from django.conf import settings
from django.test.signals import setting_changed
Expand Down Expand Up @@ -63,7 +63,7 @@


class APISettings(_APISettings): # pragma: no cover
def __check_user_settings(self, user_settings: Dict[str, Any]) -> Dict[str, Any]:
def __check_user_settings(self, user_settings: dict[str, Any]) -> dict[str, Any]:
SETTINGS_DOC = "https://django-rest-framework-simplejwt.readthedocs.io/en/latest/settings.html"

for setting in REMOVED_SETTINGS:
Expand Down
4 changes: 2 additions & 2 deletions rest_framework_simplejwt/token_blacklist/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime
from typing import Any, List, Optional, TypeVar
from typing import Any, Optional, TypeVar

from django.contrib import admin
from django.contrib.auth.models import AbstractBaseUser
Expand Down Expand Up @@ -34,7 +34,7 @@ def get_queryset(self, *args, **kwargs) -> QuerySet:
# Read-only behavior defined below
actions = None

def get_readonly_fields(self, *args, **kwargs) -> List[Any]:
def get_readonly_fields(self, *args, **kwargs) -> list[Any]:
return [f.name for f in self.model._meta.fields]

def has_add_permission(self, *args, **kwargs) -> bool:
Expand Down
8 changes: 4 additions & 4 deletions rest_framework_simplejwt/tokens.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime, timedelta
from typing import TYPE_CHECKING, Any, Dict, Generic, Optional, Type, TypeVar
from typing import TYPE_CHECKING, Any, Generic, Optional, TypeVar
from uuid import uuid4

from django.conf import settings
Expand Down Expand Up @@ -204,7 +204,7 @@ def check_exp(
raise TokenError(format_lazy(_("Token '{}' claim has expired"), claim))

@classmethod
def for_user(cls: Type[T], user: AuthUser) -> T:
def for_user(cls: type[T], user: AuthUser) -> T:
"""
Returns an authorization token for the given user that will be provided
after authenticating the user's credentials.
Expand Down Expand Up @@ -246,7 +246,7 @@ class BlacklistMixin(Generic[T]):
membership in a token blacklist.
"""

payload: Dict[str, Any]
payload: dict[str, Any]

if "rest_framework_simplejwt.token_blacklist" in settings.INSTALLED_APPS:

Expand Down Expand Up @@ -288,7 +288,7 @@ def blacklist(self) -> BlacklistedToken:
return BlacklistedToken.objects.get_or_create(token=token)

@classmethod
def for_user(cls: Type[T], user: AuthUser) -> T:
def for_user(cls: type[T], user: AuthUser) -> T:
"""
Adds this token to the outstanding token list.
"""
Expand Down
Loading