Skip to content

Commit

Permalink
bleak/__init__.py: Partial typehints (#1512)
Browse files Browse the repository at this point in the history
Fill in some missing type hints.
  • Loading branch information
Siecje committed Feb 14, 2024
1 parent 30c51f4 commit 48b1963
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions bleak/__init__.py
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,7 +519,7 @@ def __init__(
winrt: WinRTClientArgs = {},
backend: Optional[Type[BaseBleakClient]] = None,
**kwargs,
):
) -> None:
PlatformBleakClient = (
get_platform_client_backend_type() if backend is None else backend
)
Expand Down Expand Up @@ -553,19 +559,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 +834,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 +904,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 48b1963

Please sign in to comment.