From 281176a3fad0616bf02396991ab8d6bfffff0daf Mon Sep 17 00:00:00 2001 From: Carter Tinney Date: Tue, 23 Jan 2024 11:52:25 -0800 Subject: [PATCH] added future annotations --- .pre-commit-config.yaml | 2 +- .../iot/device/iothub/abstract_clients.py | 39 +++++++++++++------ .../iot/device/iothub/aio/async_clients.py | 10 +++-- .../azure/iot/device/iothub/sync_clients.py | 16 +++++--- .../aio/async_provisioning_device_client.py | 2 +- .../provisioning_device_client.py | 1 + setup.py | 1 + 7 files changed, 50 insertions(+), 21 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8c2296054..059c326f2 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -4,7 +4,7 @@ repos: hooks: - id: black language_version: python3 -- repo: https://gitlab.com/pycqa/flake8 +- repo: https://github.com/pycqa/flake8 rev: 3.9.1 # Use the ref you want to point at hooks: - id: flake8 diff --git a/azure-iot-device/azure/iot/device/iothub/abstract_clients.py b/azure-iot-device/azure/iot/device/iothub/abstract_clients.py index aff2ab84a..6fd44f924 100644 --- a/azure-iot-device/azure/iot/device/iothub/abstract_clients.py +++ b/azure-iot-device/azure/iot/device/iothub/abstract_clients.py @@ -5,7 +5,7 @@ # -------------------------------------------------------------------------- """This module contains abstract classes for the various clients of the Azure IoT Hub Device SDK """ - +from __future__ import annotations import abc import logging import threading @@ -238,7 +238,12 @@ def _replace_user_supplied_sastoken(self, sastoken_str: str) -> None: self._mqtt_pipeline.pipeline_configuration.sastoken = new_token_o @abc.abstractmethod - def _generic_receive_handler_setter(self, handler_name: str, feature_name: str, new_handler: Optional[FunctionOrCoroutine[[Any], Any]]) -> None: + def _generic_receive_handler_setter( + self, + handler_name: str, + feature_name: str, + new_handler: Optional[FunctionOrCoroutine[[Any], Any]], + ) -> None: # Will be implemented differently in child classes, but define here for static analysis pass @@ -315,7 +320,7 @@ def create_from_connection_string(cls, connection_string: str, **kwargs) -> Self hostname=connection_string[cs.HOST_NAME], gateway_hostname=connection_string.get(cs.GATEWAY_HOST_NAME), sastoken=sastoken, - **config_kwargs + **config_kwargs, ) if cls.__name__ == "IoTHubDeviceClient": pipeline_configuration.blob_upload = True @@ -387,7 +392,7 @@ def create_from_sastoken(cls, sastoken: str, **kwargs: Dict[str, Any]) -> Self: module_id=vals["module_id"], hostname=vals["hostname"], sastoken=sastoken_o, - **config_kwargs + **config_kwargs, ) if cls.__name__ == "IoTHubDeviceClient": pipeline_configuration.blob_upload = True # Blob Upload is a feature on Device Clients @@ -423,7 +428,9 @@ def receive_method_request(self, method_name: Optional[str] = None) -> None: pass @abc.abstractmethod - def send_method_response(self, method_request: MethodRequest, payload: Dict[str, JSONSerializable], status: int) -> None: + def send_method_response( + self, method_request: MethodRequest, payload: Dict[str, JSONSerializable], status: int + ) -> None: pass @abc.abstractmethod @@ -522,7 +529,9 @@ def on_twin_desired_properties_patch_received(self) -> FunctionOrCoroutine[[Twin return self._handler_manager.on_twin_desired_properties_patch_received @on_twin_desired_properties_patch_received.setter - def on_twin_desired_properties_patch_received(self, value: FunctionOrCoroutine[[TwinPatch], None]): + def on_twin_desired_properties_patch_received( + self, value: FunctionOrCoroutine[[TwinPatch], None] + ): self._generic_receive_handler_setter( "on_twin_desired_properties_patch_received", pipeline_constant.TWIN_PATCHES, value ) @@ -530,7 +539,9 @@ def on_twin_desired_properties_patch_received(self, value: FunctionOrCoroutine[[ class AbstractIoTHubDeviceClient(AbstractIoTHubClient): @classmethod - def create_from_x509_certificate(cls, x509: X509, hostname: str, device_id: str, **kwargs) -> Self: + def create_from_x509_certificate( + cls, x509: X509, hostname: str, device_id: str, **kwargs + ) -> Self: """ Instantiate a client using X509 certificate authentication. @@ -592,7 +603,9 @@ def create_from_x509_certificate(cls, x509: X509, hostname: str, device_id: str, return cls(mqtt_pipeline, http_pipeline) @classmethod - def create_from_symmetric_key(cls, symmetric_key: str, hostname: str, device_id: str, **kwargs) -> Self: + def create_from_symmetric_key( + cls, symmetric_key: str, hostname: str, device_id: str, **kwargs + ) -> Self: """ Instantiate a client using symmetric key authentication. @@ -815,7 +828,7 @@ def create_from_edge_environment(cls, **kwargs) -> Self: gateway_hostname=gateway_hostname, sastoken=sastoken, server_verification_cert=server_verification_cert, - **config_kwargs + **config_kwargs, ) pipeline_configuration.ensure_desired_properties = True @@ -830,7 +843,9 @@ def create_from_edge_environment(cls, **kwargs) -> Self: return cls(mqtt_pipeline, http_pipeline) @classmethod - def create_from_x509_certificate(cls, x509: X509, hostname: str, device_id: str, module_id: str, **kwargs) -> Self: + def create_from_x509_certificate( + cls, x509: X509, hostname: str, device_id: str, module_id: str, **kwargs + ) -> Self: """ Instantiate a client using X509 certificate authentication. @@ -899,7 +914,9 @@ def receive_message_on_input(self, input_name: str) -> Message: pass @abc.abstractmethod - def invoke_method(self, method_params: dict, device_id: str, module_id: Optional[str] = None) -> None: + def invoke_method( + self, method_params: dict, device_id: str, module_id: Optional[str] = None + ) -> None: pass @property diff --git a/azure-iot-device/azure/iot/device/iothub/aio/async_clients.py b/azure-iot-device/azure/iot/device/iothub/aio/async_clients.py index 457b299ac..88ea8987f 100644 --- a/azure-iot-device/azure/iot/device/iothub/aio/async_clients.py +++ b/azure-iot-device/azure/iot/device/iothub/aio/async_clients.py @@ -6,7 +6,7 @@ """This module contains user-facing asynchronous clients for the Azure IoTHub Device SDK for Python. """ - +from __future__ import annotations import logging import asyncio import deprecation @@ -134,7 +134,9 @@ async def _disable_feature(self, feature_name: str) -> None: # This branch shouldn't be reached, but in case it is, log it logger.info("Feature ({}) already disabled - skipping".format(feature_name)) - def _generic_receive_handler_setter(self, handler_name: str, feature_name: str, new_handler: FunctionOrCoroutine[[None], None]) -> None: + def _generic_receive_handler_setter( + self, handler_name: str, feature_name: str, new_handler: FunctionOrCoroutine[[None], None] + ) -> None: """Set a receive handler on the handler manager and enable the corresponding feature. This is a synchronous call (yes, even though this is the async client), meaning that this @@ -688,7 +690,9 @@ async def receive_message_on_input(self, input_name: str) -> Message: logger.info("Input message received on: " + input_name) return message - async def invoke_method(self, method_params, device_id, module_id: Optional[str] = None) -> MethodResponse: + async def invoke_method( + self, method_params, device_id, module_id: Optional[str] = None + ) -> MethodResponse: """Invoke a method from your client onto a device or module client, and receive the response to the method call. :param dict method_params: Should contain a methodName (str), payload (str), diff --git a/azure-iot-device/azure/iot/device/iothub/sync_clients.py b/azure-iot-device/azure/iot/device/iothub/sync_clients.py index 8748712fe..98d9b8104 100644 --- a/azure-iot-device/azure/iot/device/iothub/sync_clients.py +++ b/azure-iot-device/azure/iot/device/iothub/sync_clients.py @@ -6,7 +6,7 @@ """This module contains user-facing synchronous clients for the Azure IoTHub Device SDK for Python. """ - +from __future__ import annotations import logging import deprecation from .abstract_clients import ( @@ -25,7 +25,7 @@ from azure.iot.device import constant as device_constant from .pipeline import MQTTPipeline, HTTPPipeline from azure.iot.device.custom_typing import FunctionOrCoroutine, StorageInfo, Twin, TwinPatch -from typing import Any, Optional, Union +from typing import Optional, Union logger = logging.getLogger(__name__) @@ -134,7 +134,9 @@ def _disable_feature(self, feature_name: str) -> None: # This branch shouldn't be reached, but in case it is, log it logger.info("Feature ({}) already disabled - skipping".format(feature_name)) - def _generic_receive_handler_setter(self, handler_name: str, feature_name: str, new_handler: FunctionOrCoroutine[[None], None]) -> None: + def _generic_receive_handler_setter( + self, handler_name: str, feature_name: str, new_handler: FunctionOrCoroutine[[None], None] + ) -> None: """Set a receive handler on the handler manager and enable the corresponding feature. This is a synchronous call, meaning that this function will not return until the feature @@ -354,7 +356,9 @@ def send_message(self, message: Union[Message, str]) -> None: current_version=device_constant.VERSION, details="We recommend that you use the .on_method_request_received property to set a handler instead", ) - def receive_method_request(self, method_name: Optional[str] = None, block: bool = True, timeout: Optional[int] = None) -> MethodRequest: + def receive_method_request( + self, method_name: Optional[str] = None, block: bool = True, timeout: Optional[int] = None + ) -> MethodRequest: """Receive a method request via the Azure IoT Hub or Azure IoT Edge Hub. :param str method_name: Optionally provide the name of the method to receive requests for. @@ -675,7 +679,9 @@ def send_message_to_output(self, message: Message, output_name: str) -> None: current_version=device_constant.VERSION, details="We recommend that you use the .on_message_received property to set a handler instead", ) - def receive_message_on_input(self, input_name: str, block: bool = True, timeout: Optional[int] = None) -> Message: + def receive_message_on_input( + self, input_name: str, block: bool = True, timeout: Optional[int] = None + ) -> Message: """Receive an input message that has been sent from another Module to a specific input. :param str input_name: The input name to receive a message on. diff --git a/azure-iot-device/azure/iot/device/provisioning/aio/async_provisioning_device_client.py b/azure-iot-device/azure/iot/device/provisioning/aio/async_provisioning_device_client.py index 3c50a8d50..a96032191 100644 --- a/azure-iot-device/azure/iot/device/provisioning/aio/async_provisioning_device_client.py +++ b/azure-iot-device/azure/iot/device/provisioning/aio/async_provisioning_device_client.py @@ -8,7 +8,7 @@ Device SDK. This client uses Symmetric Key and X509 authentication to register devices with an IoT Hub via the Device Provisioning Service. """ - +from __future__ import annotations import logging from typing import Any from azure.iot.device.common import async_adapter diff --git a/azure-iot-device/azure/iot/device/provisioning/provisioning_device_client.py b/azure-iot-device/azure/iot/device/provisioning/provisioning_device_client.py index df31399dc..fb953b7a4 100644 --- a/azure-iot-device/azure/iot/device/provisioning/provisioning_device_client.py +++ b/azure-iot-device/azure/iot/device/provisioning/provisioning_device_client.py @@ -8,6 +8,7 @@ Device SDK. This client uses Symmetric Key and X509 authentication to register devices with an IoT Hub via the Device Provisioning Service. """ +from __future__ import annotations import logging from typing import Any from azure.iot.device.common.evented_callback import EventedCallback diff --git a/setup.py b/setup.py index 92f0bb326..07bece21b 100644 --- a/setup.py +++ b/setup.py @@ -85,6 +85,7 @@ "requests-unixsocket>=0.1.5,<1.0.0", "janus", "PySocks", + "typing_extensions", ], python_requires=">=3.6, <4", packages=find_namespace_packages(where="azure-iot-device"),