Skip to content

Commit 23f7ca7

Browse files
authored
Merge pull request #34 from ronnie-llamado/feature/add-type-annotations
Add type annotations
2 parents e3188ad + 331dfa0 commit 23f7ca7

File tree

4 files changed

+80
-49
lines changed

4 files changed

+80
-49
lines changed

adafruit_azureiot/device_registration.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class DeviceRegistrationError(Exception):
2828
An error from the device registration
2929
"""
3030

31-
def __init__(self, message):
31+
def __init__(self, message: str):
3232
super().__init__(message)
3333
self.message = message
3434

adafruit_azureiot/hmac.py

+30-21
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@
1616

1717
# pylint: disable=C0103, W0108, R0915, C0116, C0115
1818

19+
try:
20+
from typing import Union
21+
except ImportError:
22+
pass
1923

20-
def __translate(key, translation):
24+
25+
def __translate(key: Union[bytes, bytearray], translation: bytes) -> bytes:
2126
return bytes(translation[x] for x in key)
2227

2328

@@ -28,7 +33,7 @@ def __translate(key, translation):
2833
SHA_DIGESTSIZE = 32
2934

3035

31-
def new_shaobject():
36+
def new_shaobject() -> dict:
3237
"""Struct. for storing SHA information."""
3338
return {
3439
"digest": [0] * 8,
@@ -40,7 +45,7 @@ def new_shaobject():
4045
}
4146

4247

43-
def sha_init():
48+
def sha_init() -> dict:
4449
"""Initialize the SHA digest."""
4550
sha_info = new_shaobject()
4651
sha_info["digest"] = [
@@ -73,7 +78,7 @@ def sha_init():
7378
Gamma1 = lambda x: (S(x, 17) ^ S(x, 19) ^ R(x, 10))
7479

7580

76-
def sha_transform(sha_info):
81+
def sha_transform(sha_info: dict) -> None:
7782
W = []
7883

7984
d = sha_info["data"]
@@ -90,7 +95,7 @@ def sha_transform(sha_info):
9095
ss = sha_info["digest"][:]
9196

9297
# pylint: disable=too-many-arguments, line-too-long
93-
def RND(a, b, c, d, e, f, g, h, i, ki):
98+
def RND(a, b, c, d, e, f, g, h, i, ki): # type: ignore[no-untyped-def]
9499
"""Compress"""
95100
t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]
96101
t1 = Sigma0(a) + Maj(a, b, c)
@@ -298,7 +303,7 @@ def RND(a, b, c, d, e, f, g, h, i, ki):
298303
sha_info["digest"] = dig
299304

300305

301-
def sha_update(sha_info, buffer):
306+
def sha_update(sha_info: dict, buffer: Union[bytes, bytearray]) -> None:
302307
"""Update the SHA digest.
303308
:param dict sha_info: SHA Digest.
304309
:param str buffer: SHA buffer size.
@@ -346,13 +351,13 @@ def sha_update(sha_info, buffer):
346351
sha_info["local"] = count
347352

348353

349-
def getbuf(s):
354+
def getbuf(s: Union[str, bytes, bytearray]) -> Union[bytes, bytearray]:
350355
if isinstance(s, str):
351356
return s.encode("ascii")
352357
return bytes(s)
353358

354359

355-
def sha_final(sha_info):
360+
def sha_final(sha_info: dict) -> bytes:
356361
"""Finish computing the SHA Digest."""
357362
lo_bit_count = sha_info["count_lo"]
358363
hi_bit_count = sha_info["count_hi"]
@@ -393,28 +398,28 @@ class sha256:
393398
block_size = SHA_BLOCKSIZE
394399
name = "sha256"
395400

396-
def __init__(self, s=None):
401+
def __init__(self, s: Union[str, bytes, bytearray] = None):
397402
"""Constructs a SHA256 hash object."""
398403
self._sha = sha_init()
399404
if s:
400405
sha_update(self._sha, getbuf(s))
401406

402-
def update(self, s):
407+
def update(self, s: Union[str, bytes, bytearray]) -> None:
403408
"""Updates the hash object with a bytes-like object, s."""
404409
sha_update(self._sha, getbuf(s))
405410

406-
def digest(self):
411+
def digest(self) -> bytes:
407412
"""Returns the digest of the data passed to the update()
408413
method so far."""
409414
return sha_final(self._sha.copy())[: self._sha["digestsize"]]
410415

411-
def hexdigest(self):
416+
def hexdigest(self) -> str:
412417
"""Like digest() except the digest is returned as a string object of
413418
double length, containing only hexadecimal digits.
414419
"""
415420
return "".join(["%.2x" % i for i in self.digest()])
416421

417-
def copy(self):
422+
def copy(self) -> "sha256":
418423
"""Return a copy (“clone”) of the hash object."""
419424
new = sha256()
420425
new._sha = self._sha.copy()
@@ -429,7 +434,9 @@ class HMAC:
429434

430435
blocksize = 64 # 512-bit HMAC; can be changed in subclasses.
431436

432-
def __init__(self, key, msg=None):
437+
def __init__(
438+
self, key: Union[bytes, bytearray], msg: Union[bytes, bytearray] = None
439+
):
433440
"""Create a new HMAC object.
434441
435442
key: key for the keyed hash object.
@@ -478,15 +485,15 @@ def __init__(self, key, msg=None):
478485
self.update(msg)
479486

480487
@property
481-
def name(self):
488+
def name(self) -> str:
482489
"""Return the name of this object"""
483490
return "hmac-" + self.inner.name
484491

485-
def update(self, msg):
492+
def update(self, msg: Union[bytes, bytearray]) -> None:
486493
"""Update this hashing object with the string msg."""
487494
self.inner.update(msg)
488495

489-
def copy(self):
496+
def copy(self) -> "HMAC":
490497
"""Return a separate copy of this hashing object.
491498
492499
An update to this copy won't affect the original object.
@@ -499,7 +506,7 @@ def copy(self):
499506
other.outer = self.outer.copy()
500507
return other
501508

502-
def _current(self):
509+
def _current(self) -> "sha256":
503510
"""Return a hash object for the current state.
504511
505512
To be used only internally with digest() and hexdigest().
@@ -508,7 +515,7 @@ def _current(self):
508515
hmac.update(self.inner.digest())
509516
return hmac
510517

511-
def digest(self):
518+
def digest(self) -> bytes:
512519
"""Return the hash value of this hashing object.
513520
514521
This returns a string containing 8-bit data. The object is
@@ -518,13 +525,15 @@ def digest(self):
518525
hmac = self._current()
519526
return hmac.digest()
520527

521-
def hexdigest(self):
528+
def hexdigest(self) -> str:
522529
"""Like digest(), but returns a string of hexadecimal digits instead."""
523530
hmac = self._current()
524531
return hmac.hexdigest()
525532

526533

527-
def new_hmac(key, msg=None):
534+
def new_hmac(
535+
key: Union[bytes, bytearray], msg: Union[bytes, bytearray] = None
536+
) -> "HMAC":
528537
"""Create a new hashing object and return it.
529538
530539
key: The starting key for the hash.

adafruit_azureiot/iothub_device.py

+33-17
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@
1212
* Author(s): Jim Bennett, Elena Horton
1313
"""
1414

15+
try:
16+
from typing import Any, Callable, Mapping, Union
17+
except ImportError:
18+
pass
19+
1520
import json
1621
import adafruit_logging as logging
1722
from .iot_error import IoTError
1823
from .iot_mqtt import IoTMQTT, IoTMQTTCallback, IoTResponse
1924

2025

21-
def _validate_keys(connection_string_parts):
26+
def _validate_keys(connection_string_parts: Mapping) -> None:
2227
"""Raise ValueError if incorrect combination of keys"""
2328
host_name = connection_string_parts.get(HOST_NAME)
2429
shared_access_key_name = connection_string_parts.get(SHARED_ACCESS_KEY_NAME)
@@ -67,7 +72,7 @@ def connection_status_change(self, connected: bool) -> None:
6772
self._on_connection_status_changed(connected)
6873

6974
# pylint: disable=W0613, R0201
70-
def direct_method_invoked(self, method_name: str, payload) -> IoTResponse:
75+
def direct_method_invoked(self, method_name: str, payload: str) -> IoTResponse:
7176
"""Called when a direct method is invoked
7277
:param str method_name: The name of the method that was invoked
7378
:param str payload: The payload with the message
@@ -91,7 +96,10 @@ def cloud_to_device_message_received(self, body: str, properties: dict) -> None:
9196
self._on_cloud_to_device_message_received(body, properties)
9297

9398
def device_twin_desired_updated(
94-
self, desired_property_name: str, desired_property_value, desired_version: int
99+
self,
100+
desired_property_name: str,
101+
desired_property_value: Any,
102+
desired_version: int,
95103
) -> None:
96104
"""Called when the device twin desired properties are updated
97105
:param str desired_property_name: The name of the desired property that was updated
@@ -107,7 +115,7 @@ def device_twin_desired_updated(
107115
def device_twin_reported_updated(
108116
self,
109117
reported_property_name: str,
110-
reported_property_value,
118+
reported_property_value: Any,
111119
reported_version: int,
112120
) -> None:
113121
"""Called when the device twin reported values are updated
@@ -175,21 +183,23 @@ def __init__(
175183
self._mqtt = None
176184

177185
@property
178-
def on_connection_status_changed(self):
186+
def on_connection_status_changed(self) -> Callable:
179187
"""A callback method that is called when the connection status is changed. This method should have the following signature:
180188
def connection_status_changed(connected: bool) -> None
181189
"""
182190
return self._on_connection_status_changed
183191

184192
@on_connection_status_changed.setter
185-
def on_connection_status_changed(self, new_on_connection_status_changed):
193+
def on_connection_status_changed(
194+
self, new_on_connection_status_changed: Callable
195+
) -> None:
186196
"""A callback method that is called when the connection status is changed. This method should have the following signature:
187197
def connection_status_changed(connected: bool) -> None
188198
"""
189199
self._on_connection_status_changed = new_on_connection_status_changed
190200

191201
@property
192-
def on_direct_method_invoked(self):
202+
def on_direct_method_invoked(self) -> Callable:
193203
"""A callback method that is called when a direct method is invoked. This method should have the following signature:
194204
def direct_method_invoked(method_name: str, payload: str) -> IoTResponse:
195205
@@ -202,7 +212,7 @@ def direct_method_invoked(method_name: str, payload: str) -> IoTResponse:
202212
return self._on_direct_method_invoked
203213

204214
@on_direct_method_invoked.setter
205-
def on_direct_method_invoked(self, new_on_direct_method_invoked):
215+
def on_direct_method_invoked(self, new_on_direct_method_invoked: Callable) -> None:
206216
"""A callback method that is called when a direct method is invoked. This method should have the following signature:
207217
def direct_method_invoked(method_name: str, payload: str) -> IoTResponse:
208218
@@ -215,16 +225,16 @@ def direct_method_invoked(method_name: str, payload: str) -> IoTResponse:
215225
self._on_direct_method_invoked = new_on_direct_method_invoked
216226

217227
@property
218-
def on_cloud_to_device_message_received(self):
228+
def on_cloud_to_device_message_received(self) -> Callable:
219229
"""A callback method that is called when a cloud to device message is received. This method should have the following signature:
220230
def cloud_to_device_message_received(body: str, properties: dict) -> None:
221231
"""
222232
return self._on_cloud_to_device_message_received
223233

224234
@on_cloud_to_device_message_received.setter
225235
def on_cloud_to_device_message_received(
226-
self, new_on_cloud_to_device_message_received
227-
):
236+
self, new_on_cloud_to_device_message_received: Callable
237+
) -> None:
228238
"""A callback method that is called when a cloud to device message is received. This method should have the following signature:
229239
def cloud_to_device_message_received(body: str, properties: dict) -> None:
230240
"""
@@ -233,15 +243,17 @@ def cloud_to_device_message_received(body: str, properties: dict) -> None:
233243
)
234244

235245
@property
236-
def on_device_twin_desired_updated(self):
246+
def on_device_twin_desired_updated(self) -> Callable:
237247
"""A callback method that is called when the desired properties of the devices device twin are updated.
238248
This method should have the following signature:
239249
def device_twin_desired_updated(desired_property_name: str, desired_property_value, desired_version: int) -> None:
240250
"""
241251
return self._on_device_twin_desired_updated
242252

243253
@on_device_twin_desired_updated.setter
244-
def on_device_twin_desired_updated(self, new_on_device_twin_desired_updated):
254+
def on_device_twin_desired_updated(
255+
self, new_on_device_twin_desired_updated: Callable
256+
) -> None:
245257
"""A callback method that is called when the desired properties of the devices device twin are updated.
246258
This method should have the following signature:
247259
def device_twin_desired_updated(desired_property_name: str, desired_property_value, desired_version: int) -> None:
@@ -252,15 +264,17 @@ def device_twin_desired_updated(desired_property_name: str, desired_property_val
252264
self._mqtt.subscribe_to_twins()
253265

254266
@property
255-
def on_device_twin_reported_updated(self):
267+
def on_device_twin_reported_updated(self) -> Callable:
256268
"""A callback method that is called when the reported properties of the devices device twin are updated.
257269
This method should have the following signature:
258270
def device_twin_reported_updated(reported_property_name: str, reported_property_value, reported_version: int) -> None:
259271
"""
260272
return self._on_device_twin_reported_updated
261273

262274
@on_device_twin_reported_updated.setter
263-
def on_device_twin_reported_updated(self, new_on_device_twin_reported_updated):
275+
def on_device_twin_reported_updated(
276+
self, new_on_device_twin_reported_updated: Callable
277+
) -> None:
264278
"""A callback method that is called when the reported properties of the devices device twin are updated.
265279
This method should have the following signature:
266280
def device_twin_reported_updated(reported_property_name: str, reported_property_value, reported_version: int) -> None:
@@ -327,7 +341,9 @@ def is_connected(self) -> bool:
327341

328342
return False
329343

330-
def send_device_to_cloud_message(self, message, system_properties=None) -> None:
344+
def send_device_to_cloud_message(
345+
self, message: Union[str, dict], system_properties: dict = None
346+
) -> None:
331347
"""Send a device to cloud message from this device to Azure IoT Hub
332348
:param message: The message data as a JSON string or a dictionary
333349
:param system_properties: System properties to send with the message
@@ -339,7 +355,7 @@ def send_device_to_cloud_message(self, message, system_properties=None) -> None:
339355

340356
self._mqtt.send_device_to_cloud_message(message, system_properties)
341357

342-
def update_twin(self, patch) -> None:
358+
def update_twin(self, patch: Union[str, dict]) -> None:
343359
"""Updates the reported properties in the devices device twin
344360
:param patch: The JSON patch to apply to the device twin reported properties
345361
"""

0 commit comments

Comments
 (0)