Skip to content

Commit 5141250

Browse files
committed
Partial typehints for bleak/__init__.py
1 parent f1b8425 commit 5141250

File tree

1 file changed

+34
-23
lines changed

1 file changed

+34
-23
lines changed

bleak/__init__.py

+34-23
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
)
3333
from warnings import warn
3434
from typing import Literal
35+
from types import TracebackType
3536

3637
if sys.version_info < (3, 12):
3738
from typing_extensions import Buffer
@@ -140,7 +141,7 @@ def __init__(
140141
cb: CBScannerArgs = {},
141142
backend: Optional[Type[BaseBleakScanner]] = None,
142143
**kwargs,
143-
):
144+
) -> None:
144145
PlatformBleakScanner = (
145146
get_platform_scanner_backend_type() if backend is None else backend
146147
)
@@ -154,11 +155,16 @@ def __init__(
154155
**kwargs,
155156
)
156157

157-
async def __aenter__(self):
158+
async def __aenter__(self) -> "BleakScanner":
158159
await self._backend.start()
159160
return self
160161

161-
async def __aexit__(self, exc_type, exc_val, exc_tb):
162+
async def __aexit__(
163+
self,
164+
exc_type: Type[BaseException],
165+
exc_val: BaseException,
166+
exc_tb: TracebackType,
167+
) -> None:
162168
await self._backend.stop()
163169

164170
def register_detection_callback(
@@ -193,15 +199,15 @@ def register_detection_callback(
193199
unregister = self._backend.register_detection_callback(callback)
194200
setattr(self, "_unregister_", unregister)
195201

196-
async def start(self):
202+
async def start(self) -> None:
197203
"""Start scanning for devices"""
198204
await self._backend.start()
199205

200-
async def stop(self):
206+
async def stop(self) -> None:
201207
"""Stop scanning for devices"""
202208
await self._backend.stop()
203209

204-
def set_scanning_filter(self, **kwargs):
210+
def set_scanning_filter(self, **kwargs) -> None:
205211
"""
206212
Set scanning filter for the BleakScanner.
207213
@@ -281,15 +287,13 @@ class ExtraArgs(TypedDict, total=False):
281287
@classmethod
282288
async def discover(
283289
cls, timeout: float = 5.0, *, return_adv: Literal[False] = False, **kwargs
284-
) -> List[BLEDevice]:
285-
...
290+
) -> List[BLEDevice]: ...
286291

287292
@overload
288293
@classmethod
289294
async def discover(
290295
cls, timeout: float = 5.0, *, return_adv: Literal[True], **kwargs
291-
) -> Dict[str, Tuple[BLEDevice, AdvertisementData]]:
292-
...
296+
) -> Dict[str, Tuple[BLEDevice, AdvertisementData]]: ...
293297

294298
@classmethod
295299
async def discover(
@@ -513,19 +517,21 @@ def __init__(
513517
winrt: WinRTClientArgs = {},
514518
backend: Optional[Type[BaseBleakClient]] = None,
515519
**kwargs,
516-
):
520+
) -> None:
517521
PlatformBleakClient = (
518522
get_platform_client_backend_type() if backend is None else backend
519523
)
520524

521525
self._backend = PlatformBleakClient(
522526
address_or_ble_device,
523-
disconnected_callback=None
524-
if disconnected_callback is None
525-
else functools.partial(disconnected_callback, self),
526-
services=None
527-
if services is None
528-
else set(map(normalize_uuid_str, services)),
527+
disconnected_callback=(
528+
None
529+
if disconnected_callback is None
530+
else functools.partial(disconnected_callback, self)
531+
),
532+
services=(
533+
None if services is None else set(map(normalize_uuid_str, services))
534+
),
529535
timeout=timeout,
530536
winrt=winrt,
531537
**kwargs,
@@ -553,19 +559,24 @@ def mtu_size(self) -> int:
553559
"""
554560
return self._backend.mtu_size
555561

556-
def __str__(self):
562+
def __str__(self) -> str:
557563
return f"{self.__class__.__name__}, {self.address}"
558564

559-
def __repr__(self):
565+
def __repr__(self) -> str:
560566
return f"<{self.__class__.__name__}, {self.address}, {type(self._backend)}>"
561567

562568
# Async Context managers
563569

564-
async def __aenter__(self):
570+
async def __aenter__(self) -> "BleakClient":
565571
await self.connect()
566572
return self
567573

568-
async def __aexit__(self, exc_type, exc_val, exc_tb):
574+
async def __aexit__(
575+
self,
576+
exc_type: Type[BaseException],
577+
exc_val: BaseException,
578+
exc_tb: TracebackType,
579+
) -> None:
569580
await self.disconnect()
570581

571582
# Connectivity methods
@@ -823,7 +834,7 @@ def callback(sender: BleakGATTCharacteristic, data: bytearray):
823834

824835
if inspect.iscoroutinefunction(callback):
825836

826-
def wrapped_callback(data):
837+
def wrapped_callback(data: bytearray) -> None:
827838
task = asyncio.create_task(callback(characteristic, data))
828839
_background_tasks.add(task)
829840
task.add_done_callback(_background_tasks.discard)
@@ -893,7 +904,7 @@ def discover(*args, **kwargs):
893904
return BleakScanner.discover(*args, **kwargs)
894905

895906

896-
def cli():
907+
def cli() -> None:
897908
import argparse
898909

899910
parser = argparse.ArgumentParser(

0 commit comments

Comments
 (0)