Skip to content

Commit

Permalink
added future annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
cartertinney committed Jan 23, 2024
1 parent 000de95 commit 281176a
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 28 additions & 11 deletions azure-iot-device/azure/iot/device/iothub/abstract_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -522,15 +529,19 @@ 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
)


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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions azure-iot-device/azure/iot/device/iothub/aio/async_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
16 changes: 11 additions & 5 deletions azure-iot-device/azure/iot/device/iothub/sync_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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__)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit 281176a

Please sign in to comment.