Skip to content

Commit

Permalink
Additional typehints for bluezdbus code (#1517)
Browse files Browse the repository at this point in the history
  • Loading branch information
Siecje authored Apr 28, 2024
1 parent d4ea9b6 commit c6907d2
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion bleak/backends/bluezdbus/characteristic.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def get_descriptor(
except StopIteration:
return None

def add_descriptor(self, descriptor: BleakGATTDescriptor):
def add_descriptor(self, descriptor: BleakGATTDescriptor) -> None:
"""Add a :py:class:`~BleakGATTDescriptor` to the characteristic.
Should not be used by end user, but rather by `bleak` itself.
Expand Down
6 changes: 3 additions & 3 deletions bleak/backends/bluezdbus/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def _get_device_property(

return value

async def async_init(self):
async def async_init(self) -> None:
"""
Connects to the D-Bus message bus and begins monitoring signals.
Expand Down Expand Up @@ -552,7 +552,7 @@ async def passive_scan(
# won't use the monitor
self._bus.export(monitor_path, monitor)

async def stop():
async def stop() -> None:
# need to remove callbacks first, otherwise we get TxPower
# and RSSI properties removed during stop which causes
# incorrect advertisement data callbacks
Expand Down Expand Up @@ -868,7 +868,7 @@ def _wait_condition_callback(new_value: Optional[Any]) -> None:
if not device_callbacks:
del condition_callbacks[device_path]

def _parse_msg(self, message: Message):
def _parse_msg(self, message: Message) -> None:
"""
Handles callbacks from dbus_fast.
"""
Expand Down
10 changes: 6 additions & 4 deletions bleak/backends/bluezdbus/service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List
from typing import Any, List

from ..service import BleakGATTService
from .characteristic import BleakGATTCharacteristicBlueZDBus
Expand All @@ -8,7 +8,7 @@
class BleakGATTServiceBlueZDBus(BleakGATTService):
"""GATT Service implementation for the BlueZ DBus backend"""

def __init__(self, obj, path):
def __init__(self, obj: Any, path: str):
super().__init__(obj)
self.__characteristics = []
self.__path = path
Expand All @@ -29,14 +29,16 @@ def characteristics(self) -> List[BleakGATTCharacteristicBlueZDBus]:
"""List of characteristics for this service"""
return self.__characteristics

def add_characteristic(self, characteristic: BleakGATTCharacteristicBlueZDBus):
def add_characteristic(
self, characteristic: BleakGATTCharacteristicBlueZDBus
) -> None:
"""Add a :py:class:`~BleakGATTCharacteristicBlueZDBus` to the service.
Should not be used by end user, but rather by `bleak` itself.
"""
self.__characteristics.append(characteristic)

@property
def path(self):
def path(self) -> str:
"""The DBus path. Mostly needed by `bleak`, not by end user"""
return self.__path
7 changes: 4 additions & 3 deletions bleak/backends/bluezdbus/signals.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import annotations

import re
from typing import Any, Coroutine, Dict, Optional
Expand All @@ -19,7 +20,7 @@


class InvalidMessageTypeError(TypeError):
def __init__(self, type):
def __init__(self, type: str):
super().__init__(f"invalid message type: {type}")


Expand All @@ -37,7 +38,7 @@ def is_message_type_valid(type: str) -> bool:
return type in _message_types


def assert_bus_name_valid(type: str):
def assert_bus_name_valid(type: str) -> None:
"""Raise an error if this is not a valid message type.
.. seealso:: https://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-routing-match-rules
Expand Down Expand Up @@ -136,7 +137,7 @@ def __init__(
self.args = None

@staticmethod
def parse(rules: str):
def parse(rules: str) -> MatchRules:
return MatchRules(**dict(r.split("=") for r in rules.split(",")))

def __str__(self) -> str:
Expand Down
7 changes: 4 additions & 3 deletions bleak/backends/bluezdbus/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import os
from typing import Optional

from dbus_fast.auth import AuthExternal
from dbus_fast.constants import MessageType
Expand All @@ -8,7 +9,7 @@
from ...exc import BleakError, BleakDBusError


def assert_reply(reply: Message):
def assert_reply(reply: Message) -> None:
"""Checks that a D-Bus message is a valid reply.
Raises:
Expand All @@ -20,7 +21,7 @@ def assert_reply(reply: Message):
assert reply.message_type == MessageType.METHOD_RETURN


def extract_service_handle_from_path(path):
def extract_service_handle_from_path(path: str) -> int:
try:
return int(path[-4:], 16)
except Exception as e:
Expand Down Expand Up @@ -54,7 +55,7 @@ def device_path_from_characteristic_path(characteristic_path: str) -> str:
return characteristic_path[:37]


def get_dbus_authenticator():
def get_dbus_authenticator() -> Optional[AuthExternal]:
uid = None
try:
uid = int(os.environ.get("BLEAK_DBUS_AUTH_UID", ""))
Expand Down
2 changes: 1 addition & 1 deletion bleak/backends/bluezdbus/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
logger = logging.getLogger(__name__)


async def _get_bluetoothctl_version():
async def _get_bluetoothctl_version() -> Optional[re.Match]:
"""Get the version of bluetoothctl."""
with contextlib.suppress(Exception):
proc = await asyncio.create_subprocess_exec(
Expand Down

0 comments on commit c6907d2

Please sign in to comment.