Skip to content

Commit 4c47abc

Browse files
Add type anotation to methods and functions (#779)
* Add type anotation to methods and functions * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 87f3d0c commit 4c47abc

File tree

13 files changed

+276
-237
lines changed

13 files changed

+276
-237
lines changed

push_notifications/api/rest_framework.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
from rest_framework.response import Response
44
from rest_framework.serializers import ModelSerializer, Serializer, ValidationError
55
from rest_framework.viewsets import ModelViewSet
6-
7-
from ..fields import UNSIGNED_64BIT_INT_MAX_VALUE, hex_re
8-
from ..models import APNSDevice, GCMDevice, WebPushDevice, WNSDevice
9-
from ..settings import PUSH_NOTIFICATIONS_SETTINGS as SETTINGS
6+
from typing import Any
7+
from push_notifications.fields import UNSIGNED_64BIT_INT_MAX_VALUE, hex_re
8+
from push_notifications.models import APNSDevice, GCMDevice, WebPushDevice, WNSDevice
9+
from push_notifications.settings import PUSH_NOTIFICATIONS_SETTINGS as SETTINGS
10+
from django.db.models import QuerySet
1011

1112

1213
# Fields
@@ -15,16 +16,16 @@ class HexIntegerField(IntegerField):
1516
Store an integer represented as a hex string of form "0x01".
1617
"""
1718

18-
def to_internal_value(self, data):
19+
def to_internal_value(self, data: str | int) -> int:
1920
# validate hex string and convert it to the unsigned
2021
# integer representation for internal use
2122
try:
22-
data = int(data, 16) if type(data) != int else data
23+
data = int(data, 16) if not isinstance(data, int) else data
2324
except ValueError:
2425
raise ValidationError("Device ID is not a valid hex number")
2526
return super().to_internal_value(data)
2627

27-
def to_representation(self, value):
28+
def to_representation(self, value: int) -> int:
2829
return value
2930

3031

@@ -45,7 +46,7 @@ class APNSDeviceSerializer(ModelSerializer):
4546
class Meta(DeviceSerializerMixin.Meta):
4647
model = APNSDevice
4748

48-
def validate_registration_id(self, value):
49+
def validate_registration_id(self, value: str) -> str:
4950

5051
# https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622958-application
5152
# As of 02/2023 APNS tokens (registration_id) "are of variable length. Do not hard-code their size."
@@ -56,7 +57,7 @@ def validate_registration_id(self, value):
5657

5758

5859
class UniqueRegistrationSerializerMixin(Serializer):
59-
def validate(self, attrs):
60+
def validate(self, attrs: dict[str, Any]) -> dict[str, Any]:
6061
devices = None
6162
primary_key = None
6263
request_method = None
@@ -103,7 +104,7 @@ class Meta(DeviceSerializerMixin.Meta):
103104
)
104105
extra_kwargs = {"id": {"read_only": False, "required": False}}
105106

106-
def validate_device_id(self, value):
107+
def validate_device_id(self, value: int) -> int:
107108
# device ids are 64 bit unsigned values
108109
if value > UNSIGNED_64BIT_INT_MAX_VALUE:
109110
raise ValidationError("Device ID is out of range")
@@ -126,7 +127,7 @@ class Meta(DeviceSerializerMixin.Meta):
126127

127128
# Permissions
128129
class IsOwner(permissions.BasePermission):
129-
def has_object_permission(self, request, view, obj):
130+
def has_object_permission(self, request: Any, view: Any, obj: Any) -> bool:
130131
# must be the owner to view the object
131132
return obj.user == request.user
132133

@@ -135,7 +136,7 @@ def has_object_permission(self, request, view, obj):
135136
class DeviceViewSetMixin:
136137
lookup_field = "registration_id"
137138

138-
def create(self, request, *args, **kwargs):
139+
def create(self, request: Any, *args: Any, **kwargs: Any) -> Response:
139140
serializer = None
140141
is_update = False
141142
if SETTINGS.get("UPDATE_ON_DUPLICATE_REG_ID") and self.lookup_field in request.data:
@@ -157,12 +158,12 @@ def create(self, request, *args, **kwargs):
157158
headers = self.get_success_headers(serializer.data)
158159
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
159160

160-
def perform_create(self, serializer):
161+
def perform_create(self, serializer: ModelSerializer) -> None:
161162
if self.request.user.is_authenticated:
162163
serializer.save(user=self.request.user)
163164
return super().perform_create(serializer)
164165

165-
def perform_update(self, serializer):
166+
def perform_update(self, serializer: ModelSerializer) -> None:
166167
if self.request.user.is_authenticated:
167168
serializer.save(user=self.request.user)
168169
return super().perform_update(serializer)
@@ -171,7 +172,7 @@ def perform_update(self, serializer):
171172
class AuthorizedMixin:
172173
permission_classes = (permissions.IsAuthenticated, IsOwner)
173174

174-
def get_queryset(self):
175+
def get_queryset(self) -> QuerySet:
175176
# filter all devices to only those belonging to the current user
176177
return self.queryset.filter(user=self.request.user)
177178

push_notifications/apns.py

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
from . import models
1515
from .conf import get_manager
1616
from .exceptions import APNSError, APNSUnsupportedPriority, APNSServerError
17+
from typing import Any, Callable
1718

1819

19-
def _apns_create_socket(creds=None, application_id=None):
20+
def _apns_create_socket(creds=None, application_id=None) -> apns2_client.APNsClient:
2021
if creds is None:
2122
if not get_manager().has_auth_token_creds(application_id):
2223
cert = get_manager().get_apns_certificate(application_id)
@@ -39,9 +40,20 @@ def _apns_create_socket(creds=None, application_id=None):
3940

4041

4142
def _apns_prepare(
42-
token, alert, application_id=None, badge=None, sound=None, category=None,
43-
content_available=False, action_loc_key=None, loc_key=None, loc_args=[],
44-
extra={}, mutable_content=False, thread_id=None, url_args=None):
43+
token: str,
44+
alert: str | dict[str, Any] | None,
45+
application_id: str | None = None,
46+
badge: int | Callable | None = None,
47+
sound: str | None = None,
48+
category: str | None = None,
49+
content_available: bool = False,
50+
action_loc_key: str | None = None,
51+
loc_key: str | None = None,
52+
loc_args: str | None = [],
53+
extra: dict[str, Any] = {},
54+
mutable_content: bool = False,
55+
thread_id: str | None = None,
56+
url_args: list[str] | None = None) -> apns2_payload.Payload:
4557
if action_loc_key or loc_key or loc_args:
4658
apns2_alert = apns2_payload.PayloadAlert(
4759
body=alert if alert else {}, body_localized_key=loc_key,
@@ -59,8 +71,13 @@ def _apns_prepare(
5971

6072

6173
def _apns_send(
62-
registration_id, alert, batch=False, application_id=None, creds=None, **kwargs
63-
):
74+
registration_id: str | list[str],
75+
alert: str | dict[str, Any] | None,
76+
batch: bool = False,
77+
application_id: str | None = None,
78+
creds: apns2_credentials.Credentials | None = None,
79+
**kwargs: Any
80+
) -> dict[str, str] | None:
6481
client = _apns_create_socket(creds=creds, application_id=application_id)
6582

6683
notification_kwargs = {}
@@ -95,9 +112,16 @@ def _apns_send(
95112
get_manager().get_apns_topic(application_id=application_id),
96113
**notification_kwargs
97114
)
115+
return None
98116

99117

100-
def apns_send_message(registration_id, alert, application_id=None, creds=None, **kwargs):
118+
def apns_send_message(
119+
registration_id: str,
120+
alert: str | dict[str, Any] | None,
121+
application_id: str | None = None,
122+
creds: apns2_credentials.Credentials | None = None,
123+
**kwargs: Any
124+
) -> None:
101125
"""
102126
Sends an APNS notification to a single registration_id.
103127
This will send the notification as form data.
@@ -122,8 +146,12 @@ def apns_send_message(registration_id, alert, application_id=None, creds=None, *
122146

123147

124148
def apns_send_bulk_message(
125-
registration_ids, alert, application_id=None, creds=None, **kwargs
126-
):
149+
registration_ids: list[str],
150+
alert: str | dict[str, Any] | None,
151+
application_id: str | None = None,
152+
creds: apns2_credentials.Credentials | None = None,
153+
**kwargs: Any
154+
) -> dict[str, str]:
127155
"""
128156
Sends an APNS notification to one or more registration_ids.
129157
The registration_ids argument needs to be a list.

0 commit comments

Comments
 (0)