From e328e07f15ae7cd15adf1c7740d6e6ae4a740d32 Mon Sep 17 00:00:00 2001 From: hundredvisionsguy Date: Tue, 25 Apr 2023 12:20:27 -0700 Subject: [PATCH 1/2] #14 Adding type annotations --- adafruit_ble_apple_notification_center.py | 43 +++++++++++++---------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/adafruit_ble_apple_notification_center.py b/adafruit_ble_apple_notification_center.py index fc0d06d..0d89847 100644 --- a/adafruit_ble_apple_notification_center.py +++ b/adafruit_ble_apple_notification_center.py @@ -2,6 +2,8 @@ # # SPDX-License-Identifier: MIT +from __future__ import annotations + """ `adafruit_ble_apple_notification_center` ================================================================================ @@ -20,6 +22,11 @@ import struct import time +try: + from typing import Generator, Union, Dict +except ImportError: + pass + from adafruit_ble.services import Service from adafruit_ble.uuid import VendorUUID from adafruit_ble.characteristics.stream import StreamIn, StreamOut @@ -29,11 +36,11 @@ class _NotificationAttribute: - def __init__(self, attribute_id, *, max_length=False): + def __init__(self, attribute_id: int, *, max_length: bool=False) -> None: self._id = attribute_id self._max_length = max_length - def __get__(self, notification, cls): + def __get__(self, notification: Notification, cls) -> int: if self._id in notification._attribute_cache: return notification._attribute_cache[self._id] @@ -104,14 +111,14 @@ class Notification: def __init__( self, - notification_id, - event_flags, - category_id, - category_count, + notification_id: int, + event_flags: int, + category_id: int, + category_count: int, *, - control_point, - data_source - ): + control_point: StreamIn, + data_source: StreamOut + ) -> None: self.id = notification_id # pylint: disable=invalid-name """Integer id of the notification.""" @@ -136,12 +143,12 @@ def __init__( self.update(event_flags, category_id, category_count) - self._attribute_cache = {} + self._attribute_cache: Dict[int] = {} self.control_point = control_point self.data_source = data_source - def send_positive_action(self): + def send_positive_action(self) -> None: """Sends positive action on this notification. For example, to accept an IncomingCall.""" cmd = 2 # ANCS_CMD_PERFORM_NOTIFICATION_ACTION, uid = self.id @@ -149,7 +156,7 @@ def send_positive_action(self): buffer = struct.pack(" None: """Sends negative action on this notification. For example, to decline an IncomingCall.""" cmd = 2 # ANCS_CMD_PERFORM_NOTIFICATION_ACTION, uid = self.id @@ -157,7 +164,7 @@ def send_negative_action(self): buffer = struct.pack(" None: """Update the notification and clear the attribute cache.""" self.category_id = category_id @@ -171,7 +178,7 @@ def update(self, event_flags, category_id, category_count): self._attribute_cache = {} - def __str__(self): + def __str__(self) -> str: # pylint: disable=too-many-branches flags = [] category = None @@ -223,11 +230,11 @@ class AppleNotificationCenterService(Service): uuid=VendorUUID("9FBF120D-6301-42D9-8C58-25E699A21DBD"), buffer_size=8 * 100 ) - def __init__(self, service=None): + def __init__(self, service: Service=None) -> None: super().__init__(service=service) self._active_notifications = {} - def _update(self): + def _update(self) -> Generator[Union[int, None], None, None]: # Pylint is incorrectly inferring the type of self.notification_source so disable no-member. while self.notification_source.in_waiting > 7: # pylint: disable=no-member buffer = self.notification_source.read(8) # pylint: disable=no-member @@ -254,7 +261,7 @@ def _update(self): del self._active_notifications[nid] yield None - def wait_for_new_notifications(self, timeout=None): + def wait_for_new_notifications(self, timeout: float=None) -> Generator[Union[int, None], None, None]: """Waits for new notifications and yields them. Returns on timeout, update, disconnect or clear.""" start_time = time.monotonic() @@ -267,7 +274,7 @@ def wait_for_new_notifications(self, timeout=None): yield new_notification @property - def active_notifications(self): + def active_notifications(self) -> dict: """A dictionary of active notifications keyed by id.""" for _ in self._update(): pass From 94e62b651ce1e34c2001e8ba226576e7ac5e7214 Mon Sep 17 00:00:00 2001 From: hundredvisionsguy Date: Tue, 25 Apr 2023 13:44:54 -0700 Subject: [PATCH 2/2] Adding type annotations per Issue #14 --- adafruit_ble_apple_notification_center.py | 24 ++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/adafruit_ble_apple_notification_center.py b/adafruit_ble_apple_notification_center.py index 0d89847..d74b45f 100644 --- a/adafruit_ble_apple_notification_center.py +++ b/adafruit_ble_apple_notification_center.py @@ -2,8 +2,6 @@ # # SPDX-License-Identifier: MIT -from __future__ import annotations - """ `adafruit_ble_apple_notification_center` ================================================================================ @@ -19,11 +17,13 @@ * Adafruit's BLE library: https://github.com/adafruit/Adafruit_CircuitPython_BLE """ +from __future__ import annotations + import struct import time try: - from typing import Generator, Union, Dict + from typing import Generator, Union, Dict, Optional, Any except ImportError: pass @@ -36,11 +36,11 @@ class _NotificationAttribute: - def __init__(self, attribute_id: int, *, max_length: bool=False) -> None: + def __init__(self, attribute_id: int, *, max_length: bool = False) -> None: self._id = attribute_id self._max_length = max_length - def __get__(self, notification: Notification, cls) -> int: + def __get__(self, notification: Notification, cls: Any) -> str: if self._id in notification._attribute_cache: return notification._attribute_cache[self._id] @@ -117,7 +117,7 @@ def __init__( category_count: int, *, control_point: StreamIn, - data_source: StreamOut + data_source: StreamOut, ) -> None: self.id = notification_id # pylint: disable=invalid-name """Integer id of the notification.""" @@ -143,7 +143,7 @@ def __init__( self.update(event_flags, category_id, category_count) - self._attribute_cache: Dict[int] = {} + self._attribute_cache: Dict[int, str] = {} self.control_point = control_point self.data_source = data_source @@ -230,11 +230,11 @@ class AppleNotificationCenterService(Service): uuid=VendorUUID("9FBF120D-6301-42D9-8C58-25E699A21DBD"), buffer_size=8 * 100 ) - def __init__(self, service: Service=None) -> None: + def __init__(self, service: Service = None) -> None: super().__init__(service=service) - self._active_notifications = {} + self._active_notifications: Dict[tuple, Notification] = {} - def _update(self) -> Generator[Union[int, None], None, None]: + def _update(self) -> Generator[Union[Notification, None], None, None]: # Pylint is incorrectly inferring the type of self.notification_source so disable no-member. while self.notification_source.in_waiting > 7: # pylint: disable=no-member buffer = self.notification_source.read(8) # pylint: disable=no-member @@ -261,7 +261,9 @@ def _update(self) -> Generator[Union[int, None], None, None]: del self._active_notifications[nid] yield None - def wait_for_new_notifications(self, timeout: float=None) -> Generator[Union[int, None], None, None]: + def wait_for_new_notifications( + self, timeout: Optional[float] = None + ) -> Generator[Union[Notification, None], None, None]: """Waits for new notifications and yields them. Returns on timeout, update, disconnect or clear.""" start_time = time.monotonic()