Skip to content

Commit f2bf6b0

Browse files
committed
Type annotate characteristics/__init__.py
1 parent 6edd7aa commit f2bf6b0

File tree

1 file changed

+48
-30
lines changed

1 file changed

+48
-30
lines changed

adafruit_ble/characteristics/__init__.py

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313

1414
from ..attributes import Attribute
1515

16+
try:
17+
from typing import Optional, Type, Union, Tuple, Iterable
18+
from circuitpython_typing import ReadableBuffer
19+
from adafruit_ble.uuid import UUID
20+
from adafruit_ble.services import Service
21+
except ImportError:
22+
pass
23+
1624
__version__ = "0.0.0-auto.0"
1725
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BLE.git"
1826

@@ -75,14 +83,14 @@ class Characteristic:
7583
def __init__(
7684
self,
7785
*,
78-
uuid=None,
79-
properties=0,
80-
read_perm=Attribute.OPEN,
81-
write_perm=Attribute.OPEN,
82-
max_length=None,
83-
fixed_length=False,
84-
initial_value=None
85-
):
86+
uuid: Optional[UUID] = None,
87+
properties: int = 0,
88+
read_perm: int = Attribute.OPEN,
89+
write_perm: int = Attribute.OPEN,
90+
max_length: Optional[int] = None,
91+
fixed_length: bool = False,
92+
initial_value: Optional[ReadableBuffer] = None
93+
) -> None:
8694
self.field_name = None # Set by Service during basic binding
8795

8896
if uuid:
@@ -94,7 +102,9 @@ def __init__(
94102
self.fixed_length = fixed_length
95103
self.initial_value = initial_value
96104

97-
def _ensure_bound(self, service, initial_value=None):
105+
def _ensure_bound(
106+
self, service: Service, initial_value: Optional[bytes] = None
107+
) -> None:
98108
"""Binds the characteristic to the local Service or remote Characteristic object given."""
99109
if self.field_name in service.bleio_characteristics:
100110
return
@@ -110,7 +120,9 @@ def _ensure_bound(self, service, initial_value=None):
110120

111121
service.bleio_characteristics[self.field_name] = bleio_characteristic
112122

113-
def __bind_locally(self, service, initial_value):
123+
def __bind_locally(
124+
self, service: Service, initial_value: Optional[bytes]
125+
) -> _bleio.Characteristic:
114126
if initial_value is None:
115127
initial_value = self.initial_value
116128
if initial_value is None and self.max_length:
@@ -132,7 +144,9 @@ def __bind_locally(self, service, initial_value):
132144
write_perm=self.write_perm,
133145
)
134146

135-
def __get__(self, service, cls=None):
147+
def __get__(
148+
self, service: Optional[Service], cls: Optional[Type[Service]] = None
149+
) -> ReadableBuffer:
136150
# CircuitPython doesn't invoke descriptor protocol on obj's class,
137151
# but CPython does. In the CPython case, pretend that it doesn't.
138152
if service is None:
@@ -141,7 +155,7 @@ def __get__(self, service, cls=None):
141155
bleio_characteristic = service.bleio_characteristics[self.field_name]
142156
return bleio_characteristic.value
143157

144-
def __set__(self, service, value):
158+
def __set__(self, service: Service, value: ReadableBuffer) -> None:
145159
self._ensure_bound(service, value)
146160
if value is None:
147161
value = b""
@@ -159,14 +173,14 @@ class ComplexCharacteristic:
159173
def __init__(
160174
self,
161175
*,
162-
uuid=None,
163-
properties=0,
164-
read_perm=Attribute.OPEN,
165-
write_perm=Attribute.OPEN,
166-
max_length=20,
167-
fixed_length=False,
168-
initial_value=None
169-
):
176+
uuid: Optional[UUID] = None,
177+
properties: int = 0,
178+
read_perm: int = Attribute.OPEN,
179+
write_perm: int = Attribute.OPEN,
180+
max_length: int = 20,
181+
fixed_length: bool = False,
182+
initial_value: Optional[ReadableBuffer] = None
183+
) -> None:
170184
self.field_name = None # Set by Service during basic binding
171185

172186
if uuid:
@@ -178,7 +192,7 @@ def __init__(
178192
self.fixed_length = fixed_length
179193
self.initial_value = initial_value
180194

181-
def bind(self, service):
195+
def bind(self, service: Service) -> _bleio.Characteristic:
182196
"""Binds the characteristic to the local Service or remote Characteristic object given."""
183197
if service.remote:
184198
for characteristic in service.bleio_service.characteristics:
@@ -195,7 +209,9 @@ def bind(self, service):
195209
write_perm=self.write_perm,
196210
)
197211

198-
def __get__(self, service, cls=None):
212+
def __get__(
213+
self, service: Optional[Service], cls: Optional[Type[Service]] = None
214+
) -> Characteristic:
199215
if service is None:
200216
return self
201217
bound_object = self.bind(service)
@@ -220,12 +236,12 @@ def __init__(
220236
self,
221237
struct_format,
222238
*,
223-
uuid=None,
224-
properties=0,
225-
read_perm=Attribute.OPEN,
226-
write_perm=Attribute.OPEN,
227-
initial_value=None
228-
):
239+
uuid: Optional[UUID] = None,
240+
properties: int = 0,
241+
read_perm: int = Attribute.OPEN,
242+
write_perm: int = Attribute.OPEN,
243+
initial_value: Optional[ReadableBuffer] = None
244+
) -> None:
229245
self._struct_format = struct_format
230246
self._expected_size = struct.calcsize(struct_format)
231247
if initial_value is not None:
@@ -240,14 +256,16 @@ def __init__(
240256
write_perm=write_perm,
241257
)
242258

243-
def __get__(self, obj, cls=None):
259+
def __get__(
260+
self, obj: Optional[Service], cls: Optional[Type[Service]] = None
261+
) -> Optional[Union[Tuple, "StructCharacteristic"]]:
244262
if obj is None:
245263
return self
246264
raw_data = super().__get__(obj, cls)
247265
if len(raw_data) < self._expected_size:
248266
return None
249267
return struct.unpack(self._struct_format, raw_data)
250268

251-
def __set__(self, obj, value):
269+
def __set__(self, obj: Service, value: Iterable) -> None:
252270
encoded = struct.pack(self._struct_format, *value)
253271
super().__set__(obj, encoded)

0 commit comments

Comments
 (0)