Improve decorator type annotations [esphome] (#104878)

pull/104914/head
Marc Mueller 2023-12-02 17:57:58 +01:00 committed by GitHub
parent e9d4a02bb1
commit 559e8dfc69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 6 deletions

View File

@ -8,7 +8,7 @@ from dataclasses import dataclass, field
from functools import partial
import logging
import sys
from typing import Any, TypeVar, cast
from typing import Any, Concatenate, ParamSpec, TypeVar
import uuid
if sys.version_info < (3, 12):
@ -60,7 +60,9 @@ CCCD_INDICATE_BYTES = b"\x02\x00"
DEFAULT_MAX_WRITE_WITHOUT_RESPONSE = DEFAULT_MTU - GATT_HEADER_SIZE
_LOGGER = logging.getLogger(__name__)
_WrapFuncType = TypeVar("_WrapFuncType", bound=Callable[..., Any])
_ESPHomeClient = TypeVar("_ESPHomeClient", bound="ESPHomeClient")
_R = TypeVar("_R")
_P = ParamSpec("_P")
def mac_to_int(address: str) -> int:
@ -68,12 +70,14 @@ def mac_to_int(address: str) -> int:
return int(address.replace(":", ""), 16)
def api_error_as_bleak_error(func: _WrapFuncType) -> _WrapFuncType:
def api_error_as_bleak_error(
func: Callable[Concatenate[_ESPHomeClient, _P], Coroutine[Any, Any, _R]]
) -> Callable[Concatenate[_ESPHomeClient, _P], Coroutine[Any, Any, _R]]:
"""Define a wrapper throw esphome api errors as BleakErrors."""
async def _async_wrap_bluetooth_operation(
self: ESPHomeClient, *args: Any, **kwargs: Any
) -> Any:
self: _ESPHomeClient, *args: _P.args, **kwargs: _P.kwargs
) -> _R:
# pylint: disable=protected-access
try:
return await func(self, *args, **kwargs)
@ -107,7 +111,7 @@ def api_error_as_bleak_error(func: _WrapFuncType) -> _WrapFuncType:
except APIConnectionError as err:
raise BleakError(str(err)) from err
return cast(_WrapFuncType, _async_wrap_bluetooth_operation)
return _async_wrap_bluetooth_operation
@dataclass(slots=True)