Skip to content

Commit

Permalink
Partial typehints for bleak/__init__.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Siecje committed Feb 14, 2024
1 parent f1b8425 commit 28e06b4
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions bleak/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
)
from warnings import warn
from typing import Literal
from types import TracebackType

if sys.version_info < (3, 12):
from typing_extensions import Buffer
Expand Down Expand Up @@ -140,7 +141,7 @@ def __init__(
cb: CBScannerArgs = {},
backend: Optional[Type[BaseBleakScanner]] = None,
**kwargs,
):
) -> None:
PlatformBleakScanner = (
get_platform_scanner_backend_type() if backend is None else backend
)
Expand All @@ -154,11 +155,16 @@ def __init__(
**kwargs,
)

async def __aenter__(self):
async def __aenter__(self) -> "BleakScanner":
await self._backend.start()
return self

async def __aexit__(self, exc_type, exc_val, exc_tb):
async def __aexit__(
self,
exc_type: Type[BaseException],
exc_val: BaseException,
exc_tb: TracebackType,
) -> None:
await self._backend.stop()

def register_detection_callback(
Expand Down Expand Up @@ -193,15 +199,15 @@ def register_detection_callback(
unregister = self._backend.register_detection_callback(callback)
setattr(self, "_unregister_", unregister)

async def start(self):
async def start(self) -> None:
"""Start scanning for devices"""
await self._backend.start()

async def stop(self):
async def stop(self) -> None:
"""Stop scanning for devices"""
await self._backend.stop()

def set_scanning_filter(self, **kwargs):
def set_scanning_filter(self, **kwargs) -> None:
"""
Set scanning filter for the BleakScanner.
Expand Down Expand Up @@ -513,19 +519,21 @@ def __init__(
winrt: WinRTClientArgs = {},
backend: Optional[Type[BaseBleakClient]] = None,
**kwargs,
):
) -> None:
PlatformBleakClient = (
get_platform_client_backend_type() if backend is None else backend
)

self._backend = PlatformBleakClient(
address_or_ble_device,
disconnected_callback=None
if disconnected_callback is None
else functools.partial(disconnected_callback, self),
services=None
if services is None
else set(map(normalize_uuid_str, services)),
disconnected_callback=(
None
if disconnected_callback is None
else functools.partial(disconnected_callback, self)
),
services=(
None if services is None else set(map(normalize_uuid_str, services))
),
timeout=timeout,
winrt=winrt,
**kwargs,
Expand Down Expand Up @@ -553,19 +561,24 @@ def mtu_size(self) -> int:
"""
return self._backend.mtu_size

def __str__(self):
def __str__(self) -> str:
return f"{self.__class__.__name__}, {self.address}"

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

# Async Context managers

async def __aenter__(self):
async def __aenter__(self) -> "BleakClient":
await self.connect()
return self

async def __aexit__(self, exc_type, exc_val, exc_tb):
async def __aexit__(
self,
exc_type: Type[BaseException],
exc_val: BaseException,
exc_tb: TracebackType,
) -> None:
await self.disconnect()

# Connectivity methods
Expand Down Expand Up @@ -823,7 +836,7 @@ def callback(sender: BleakGATTCharacteristic, data: bytearray):

if inspect.iscoroutinefunction(callback):

def wrapped_callback(data):
def wrapped_callback(data: bytearray) -> None:
task = asyncio.create_task(callback(characteristic, data))
_background_tasks.add(task)
task.add_done_callback(_background_tasks.discard)
Expand Down Expand Up @@ -893,7 +906,7 @@ def discover(*args, **kwargs):
return BleakScanner.discover(*args, **kwargs)


def cli():
def cli() -> None:
import argparse

parser = argparse.ArgumentParser(
Expand Down

0 comments on commit 28e06b4

Please sign in to comment.