Skip to content

Commit c238606

Browse files
committed
Type annotate characteristics/json.py
1 parent 0fd4509 commit c238606

File tree

1 file changed

+25
-10
lines changed
  • adafruit_ble/characteristics

1 file changed

+25
-10
lines changed

adafruit_ble/characteristics/json.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,23 @@
1010
1111
"""
1212

13+
from __future__ import annotations
14+
1315
import json
1416
from . import Attribute
1517
from . import Characteristic
1618

19+
try:
20+
from typing import Optional, Any, Type, TYPE_CHECKING
21+
22+
if TYPE_CHECKING:
23+
from circuitpython_typing import ReadableBuffer
24+
from adafruit_ble.uuid import UUID
25+
from adafruit_ble.services import Service
26+
27+
except ImportError:
28+
pass
29+
1730
__version__ = "0.0.0-auto.0"
1831
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BLE.git"
1932

@@ -24,12 +37,12 @@ class JSONCharacteristic(Characteristic):
2437
def __init__(
2538
self,
2639
*,
27-
uuid=None,
28-
properties=Characteristic.READ,
29-
read_perm=Attribute.OPEN,
30-
write_perm=Attribute.OPEN,
31-
initial_value=None,
32-
):
40+
uuid: Optional[UUID] = None,
41+
properties: int = Characteristic.READ,
42+
read_perm: int = Attribute.OPEN,
43+
write_perm: int = Attribute.OPEN,
44+
initial_value: Optional[ReadableBuffer] = None,
45+
) -> None:
3346
super().__init__(
3447
uuid=uuid,
3548
properties=properties,
@@ -41,19 +54,21 @@ def __init__(
4154
)
4255

4356
@staticmethod
44-
def pack(value):
57+
def pack(value: Any) -> bytes:
4558
"""Converts a JSON serializable python value into a utf-8 encoded JSON string."""
4659
return json.dumps(value).encode("utf-8")
4760

4861
@staticmethod
49-
def unpack(value):
62+
def unpack(value: ReadableBuffer) -> Any:
5063
"""Converts a utf-8 encoded JSON string into a python value."""
5164
return json.loads(str(value, "utf-8"))
5265

53-
def __get__(self, obj, cls=None):
66+
def __get__(
67+
self, obj: Optional[Service], cls: Optional[Type[Service]] = None
68+
) -> Any:
5469
if obj is None:
5570
return self
5671
return self.unpack(super().__get__(obj, cls))
5772

58-
def __set__(self, obj, value):
73+
def __set__(self, obj: Service, value: Any) -> None:
5974
super().__set__(obj, self.pack(value))

0 commit comments

Comments
 (0)