Skip to content

Commit d7bf0af

Browse files
committed
Add type annotations for __init__.py
1 parent f5603d9 commit d7bf0af

File tree

1 file changed

+42
-34
lines changed

1 file changed

+42
-34
lines changed

adafruit_ble/__init__.py

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@
2323
from .services import Service
2424
from .advertising import Advertisement
2525

26+
try:
27+
from typing import Optional, Iterator, Union, Tuple, Literal, NoReturn
28+
from circuitpython_typing import ReadableBuffer
29+
from adafruit_ble.uuid import UUID
30+
from adafruit_ble.characteristics import Characteristic
31+
except ImportError:
32+
pass
33+
2634
__version__ = "0.0.0-auto.0"
2735
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BLE.git"
2836

@@ -36,14 +44,14 @@ class BLEConnection:
3644
3745
"""
3846

39-
def __init__(self, bleio_connection):
47+
def __init__(self, bleio_connection: _bleio.Connection) -> None:
4048
self._bleio_connection = bleio_connection
4149
# _bleio.Service objects representing services found during discovery.
4250
self._discovered_bleio_services = {}
4351
# Service objects that wrap remote services.
4452
self._constructed_services = {}
4553

46-
def _discover_remote(self, uuid):
54+
def _discover_remote(self, uuid: UUID) -> Optional[Service]:
4755
remote_service = None
4856
if uuid in self._discovered_bleio_services:
4957
remote_service = self._discovered_bleio_services[uuid]
@@ -56,7 +64,7 @@ def _discover_remote(self, uuid):
5664
self._discovered_bleio_services[uuid] = remote_service
5765
return remote_service
5866

59-
def __contains__(self, key):
67+
def __contains__(self, key: Union[UUID, Characteristic]) -> bool:
6068
"""
6169
Allows easy testing for a particular Service class or a particular UUID
6270
associated with this connection.
@@ -74,7 +82,7 @@ def __contains__(self, key):
7482
uuid = key.uuid
7583
return self._discover_remote(uuid) is not None
7684

77-
def __getitem__(self, key):
85+
def __getitem__(self, key: Union[UUID, Characteristic]) -> Optional[Service]:
7886
"""Return the Service for the given Service class or uuid, if any."""
7987
uuid = key
8088
maybe_service = False
@@ -96,17 +104,17 @@ def __getitem__(self, key):
96104
raise KeyError("{!r} object has no service {}".format(self, key))
97105

98106
@property
99-
def connected(self):
107+
def connected(self) -> bool:
100108
"""True if the connection to the peer is still active."""
101109
return self._bleio_connection.connected
102110

103111
@property
104-
def paired(self):
112+
def paired(self) -> bool:
105113
"""True if the paired to the peer."""
106114
return self._bleio_connection.paired
107115

108116
@property
109-
def connection_interval(self):
117+
def connection_interval(self) -> float:
110118
"""Time between transmissions in milliseconds. Will be multiple of 1.25ms. Lower numbers
111119
increase speed and decrease latency but increase power consumption.
112120
@@ -118,14 +126,14 @@ def connection_interval(self):
118126
return self._bleio_connection.connection_interval
119127

120128
@connection_interval.setter
121-
def connection_interval(self, value):
129+
def connection_interval(self, value: float) -> None:
122130
self._bleio_connection.connection_interval = value
123131

124-
def pair(self, *, bond=True):
132+
def pair(self, *, bond: bool = True) -> None:
125133
"""Pair to the peer to increase security of the connection."""
126134
return self._bleio_connection.pair(bond=bond)
127135

128-
def disconnect(self):
136+
def disconnect(self) -> None:
129137
"""Disconnect from peer."""
130138
self._bleio_connection.disconnect()
131139

@@ -138,7 +146,7 @@ class BLERadio:
138146
139147
It uses this library's `Advertisement` classes and the `BLEConnection` class."""
140148

141-
def __init__(self, adapter=None):
149+
def __init__(self, adapter: Optional[_bleio.Adapter] = None) -> None:
142150
"""If no adapter is supplied, use the built-in `_bleio.adapter`.
143151
If no built-in adapter is available, raise `RuntimeError`.
144152
"""
@@ -149,8 +157,8 @@ def __init__(self, adapter=None):
149157
self._connection_cache = {}
150158

151159
def start_advertising(
152-
self, advertisement, scan_response=None, interval=0.1, timeout=None
153-
):
160+
self, advertisement: Advertisement, scan_response: Optional[ReadableBuffer] = None, interval: float = 0.1, timeout: Optional[int] = None
161+
) -> None:
154162
"""
155163
Starts advertising the given advertisement.
156164
@@ -195,21 +203,21 @@ def start_advertising(
195203
timeout=0 if timeout is None else timeout,
196204
)
197205

198-
def stop_advertising(self):
206+
def stop_advertising(self) -> None:
199207
"""Stops advertising."""
200208
self._adapter.stop_advertising()
201209

202210
def start_scan(
203211
self,
204-
*advertisement_types,
205-
buffer_size=512,
206-
extended=False,
207-
timeout=None,
208-
interval=0.1,
209-
window=0.1,
210-
minimum_rssi=-80,
211-
active=True
212-
):
212+
*advertisement_types: Advertisement,
213+
buffer_size: int = 512,
214+
extended: bool = False,
215+
timeout: Optional[float] = None,
216+
interval: float = 0.1,
217+
window: float = 0.1,
218+
minimum_rssi: int = -80,
219+
active: bool = True
220+
) -> Iterator[Advertisement]:
213221
"""
214222
Starts scanning. Returns an iterator of advertisement objects of the types given in
215223
advertisement_types. The iterator will block until an advertisement is heard or the scan
@@ -269,14 +277,14 @@ def start_scan(
269277
if advertisement:
270278
yield advertisement
271279

272-
def stop_scan(self):
280+
def stop_scan(self) -> None:
273281
"""Stops any active scan.
274282
275283
The scan results iterator will return any buffered results and then raise StopIteration
276284
once empty."""
277285
self._adapter.stop_scan()
278286

279-
def connect(self, peer, *, timeout=4.0):
287+
def connect(self, peer: Union[Advertisement, _bleio.Address], *, timeout:float = 4.0) -> BLEConnection:
280288
"""
281289
Initiates a `BLEConnection` to the peer that advertised the given advertisement.
282290
@@ -293,12 +301,12 @@ def connect(self, peer, *, timeout=4.0):
293301
return self._connection_cache[connection]
294302

295303
@property
296-
def connected(self):
304+
def connected(self) -> bool:
297305
"""True if any peers are connected."""
298306
return self._adapter.connected
299307

300308
@property
301-
def connections(self):
309+
def connections(self) -> Tuple[Optional[BLEConnection], ...]:
302310
"""A tuple of active `BLEConnection` objects."""
303311
self._clean_connection_cache()
304312
connections = self._adapter.connections
@@ -311,36 +319,36 @@ def connections(self):
311319
return tuple(wrapped_connections)
312320

313321
@property
314-
def name(self):
322+
def name(self) -> str:
315323
"""The name for this device. Used in advertisements and
316324
as the Device Name in the Generic Access Service, available to a connected peer.
317325
"""
318326
return self._adapter.name
319327

320328
@name.setter
321-
def name(self, value):
329+
def name(self, value: str) -> None:
322330
self._adapter.name = value
323331

324332
@property
325-
def tx_power(self):
333+
def tx_power(self) -> Literal[0]:
326334
"""Transmit power, in dBm."""
327335
return 0
328336

329337
@tx_power.setter
330-
def tx_power(self, value):
338+
def tx_power(self, value) -> NoReturn:
331339
raise NotImplementedError("setting tx_power not yet implemented")
332340

333341
@property
334-
def address_bytes(self):
342+
def address_bytes(self) -> bytes:
335343
"""The device address, as a ``bytes()`` object of length 6."""
336344
return self._adapter.address.address_bytes
337345

338346
@property
339-
def advertising(self):
347+
def advertising(self) -> bool:
340348
"""The advertising state"""
341349
return self._adapter.advertising # pylint: disable=no-member
342350

343-
def _clean_connection_cache(self):
351+
def _clean_connection_cache(self) -> None:
344352
"""Remove cached connections that have disconnected."""
345353
for k, connection in list(self._connection_cache.items()):
346354
if not connection.connected:

0 commit comments

Comments
 (0)