Skip to content

Commit 0e631c0

Browse files
authored
Merge pull request #168 from tekktrik/doc/add-type-annotations
Add type annotations
2 parents 82c038a + 8996567 commit 0e631c0

File tree

18 files changed

+490
-216
lines changed

18 files changed

+490
-216
lines changed

.pylintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ ignore-docstrings=yes
252252
ignore-imports=yes
253253

254254
# Minimum lines number of a similarity.
255-
min-similarity-lines=12
255+
min-similarity-lines=16
256256

257257

258258
[BASIC]

adafruit_ble/__init__.py

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
building on the native `_bleio` module.
1010
1111
"""
12+
13+
from __future__ import annotations
14+
1215
# pylint: disable=wrong-import-position
16+
1317
import sys
1418

1519
if sys.implementation.name == "circuitpython" and sys.implementation.version[0] <= 4:
@@ -23,6 +27,18 @@
2327
from .services import Service
2428
from .advertising import Advertisement
2529

30+
try:
31+
from typing import Optional, Iterator, Union, Tuple, NoReturn, TYPE_CHECKING
32+
from typing_extensions import Literal
33+
34+
if TYPE_CHECKING:
35+
from circuitpython_typing import ReadableBuffer
36+
from adafruit_ble.uuid import UUID
37+
from adafruit_ble.characteristics import Characteristic
38+
39+
except ImportError:
40+
pass
41+
2642
__version__ = "0.0.0-auto.0"
2743
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BLE.git"
2844

@@ -36,14 +52,14 @@ class BLEConnection:
3652
3753
"""
3854

39-
def __init__(self, bleio_connection):
55+
def __init__(self, bleio_connection: _bleio.Connection) -> None:
4056
self._bleio_connection = bleio_connection
4157
# _bleio.Service objects representing services found during discovery.
4258
self._discovered_bleio_services = {}
4359
# Service objects that wrap remote services.
4460
self._constructed_services = {}
4561

46-
def _discover_remote(self, uuid):
62+
def _discover_remote(self, uuid: UUID) -> Optional[_bleio.Service]:
4763
remote_service = None
4864
if uuid in self._discovered_bleio_services:
4965
remote_service = self._discovered_bleio_services[uuid]
@@ -56,7 +72,7 @@ def _discover_remote(self, uuid):
5672
self._discovered_bleio_services[uuid] = remote_service
5773
return remote_service
5874

59-
def __contains__(self, key):
75+
def __contains__(self, key: Union[UUID, Service]) -> bool:
6076
"""
6177
Allows easy testing for a particular Service class or a particular UUID
6278
associated with this connection.
@@ -74,7 +90,7 @@ def __contains__(self, key):
7490
uuid = key.uuid
7591
return self._discover_remote(uuid) is not None
7692

77-
def __getitem__(self, key):
93+
def __getitem__(self, key: Union[UUID, Service]) -> Optional[Service]:
7894
"""Return the Service for the given Service class or uuid, if any."""
7995
uuid = key
8096
maybe_service = False
@@ -96,17 +112,17 @@ def __getitem__(self, key):
96112
raise KeyError("{!r} object has no service {}".format(self, key))
97113

98114
@property
99-
def connected(self):
115+
def connected(self) -> bool:
100116
"""True if the connection to the peer is still active."""
101117
return self._bleio_connection.connected
102118

103119
@property
104-
def paired(self):
120+
def paired(self) -> bool:
105121
"""True if the paired to the peer."""
106122
return self._bleio_connection.paired
107123

108124
@property
109-
def connection_interval(self):
125+
def connection_interval(self) -> float:
110126
"""Time between transmissions in milliseconds. Will be multiple of 1.25ms. Lower numbers
111127
increase speed and decrease latency but increase power consumption.
112128
@@ -118,14 +134,14 @@ def connection_interval(self):
118134
return self._bleio_connection.connection_interval
119135

120136
@connection_interval.setter
121-
def connection_interval(self, value):
137+
def connection_interval(self, value: float) -> None:
122138
self._bleio_connection.connection_interval = value
123139

124-
def pair(self, *, bond=True):
140+
def pair(self, *, bond: bool = True) -> None:
125141
"""Pair to the peer to increase security of the connection."""
126142
return self._bleio_connection.pair(bond=bond)
127143

128-
def disconnect(self):
144+
def disconnect(self) -> None:
129145
"""Disconnect from peer."""
130146
self._bleio_connection.disconnect()
131147

@@ -138,7 +154,7 @@ class BLERadio:
138154
139155
It uses this library's `Advertisement` classes and the `BLEConnection` class."""
140156

141-
def __init__(self, adapter=None):
157+
def __init__(self, adapter: Optional[_bleio.Adapter] = None) -> None:
142158
"""If no adapter is supplied, use the built-in `_bleio.adapter`.
143159
If no built-in adapter is available, raise `RuntimeError`.
144160
"""
@@ -149,8 +165,12 @@ def __init__(self, adapter=None):
149165
self._connection_cache = {}
150166

151167
def start_advertising(
152-
self, advertisement, scan_response=None, interval=0.1, timeout=None
153-
):
168+
self,
169+
advertisement: Advertisement,
170+
scan_response: Optional[ReadableBuffer] = None,
171+
interval: float = 0.1,
172+
timeout: Optional[int] = None,
173+
) -> None:
154174
"""
155175
Starts advertising the given advertisement.
156176
@@ -195,21 +215,21 @@ def start_advertising(
195215
timeout=0 if timeout is None else timeout,
196216
)
197217

198-
def stop_advertising(self):
218+
def stop_advertising(self) -> None:
199219
"""Stops advertising."""
200220
self._adapter.stop_advertising()
201221

202222
def start_scan(
203223
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-
):
224+
*advertisement_types: Advertisement,
225+
buffer_size: int = 512,
226+
extended: bool = False,
227+
timeout: Optional[float] = None,
228+
interval: float = 0.1,
229+
window: float = 0.1,
230+
minimum_rssi: int = -80,
231+
active: bool = True,
232+
) -> Iterator[Advertisement]:
213233
"""
214234
Starts scanning. Returns an iterator of advertisement objects of the types given in
215235
advertisement_types. The iterator will block until an advertisement is heard or the scan
@@ -269,14 +289,16 @@ def start_scan(
269289
if advertisement:
270290
yield advertisement
271291

272-
def stop_scan(self):
292+
def stop_scan(self) -> None:
273293
"""Stops any active scan.
274294
275295
The scan results iterator will return any buffered results and then raise StopIteration
276296
once empty."""
277297
self._adapter.stop_scan()
278298

279-
def connect(self, peer, *, timeout=4.0):
299+
def connect(
300+
self, peer: Union[Advertisement, _bleio.Address], *, timeout: float = 4.0
301+
) -> BLEConnection:
280302
"""
281303
Initiates a `BLEConnection` to the peer that advertised the given advertisement.
282304
@@ -293,12 +315,12 @@ def connect(self, peer, *, timeout=4.0):
293315
return self._connection_cache[connection]
294316

295317
@property
296-
def connected(self):
318+
def connected(self) -> bool:
297319
"""True if any peers are connected."""
298320
return self._adapter.connected
299321

300322
@property
301-
def connections(self):
323+
def connections(self) -> Tuple[Optional[BLEConnection], ...]:
302324
"""A tuple of active `BLEConnection` objects."""
303325
self._clean_connection_cache()
304326
connections = self._adapter.connections
@@ -311,36 +333,36 @@ def connections(self):
311333
return tuple(wrapped_connections)
312334

313335
@property
314-
def name(self):
336+
def name(self) -> str:
315337
"""The name for this device. Used in advertisements and
316338
as the Device Name in the Generic Access Service, available to a connected peer.
317339
"""
318340
return self._adapter.name
319341

320342
@name.setter
321-
def name(self, value):
343+
def name(self, value: str) -> None:
322344
self._adapter.name = value
323345

324346
@property
325-
def tx_power(self):
347+
def tx_power(self) -> Literal[0]:
326348
"""Transmit power, in dBm."""
327349
return 0
328350

329351
@tx_power.setter
330-
def tx_power(self, value):
352+
def tx_power(self, value) -> NoReturn:
331353
raise NotImplementedError("setting tx_power not yet implemented")
332354

333355
@property
334-
def address_bytes(self):
356+
def address_bytes(self) -> bytes:
335357
"""The device address, as a ``bytes()`` object of length 6."""
336358
return self._adapter.address.address_bytes
337359

338360
@property
339-
def advertising(self):
361+
def advertising(self) -> bool:
340362
"""The advertising state"""
341363
return self._adapter.advertising # pylint: disable=no-member
342364

343-
def _clean_connection_cache(self):
365+
def _clean_connection_cache(self) -> None:
344366
"""Remove cached connections that have disconnected."""
345367
for k, connection in list(self._connection_cache.items()):
346368
if not connection.connected:

0 commit comments

Comments
 (0)