Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use PEP 695 for decorator typing with type aliases (1) #117662

Merged
merged 1 commit into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions homeassistant/components/androidtv/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from collections.abc import Awaitable, Callable, Coroutine
import functools
import logging
from typing import Any, Concatenate, ParamSpec, TypeVar
from typing import Any, Concatenate

from androidtv.exceptions import LockNotAcquiredException

Expand Down Expand Up @@ -34,15 +34,13 @@

_LOGGER = logging.getLogger(__name__)

_ADBDeviceT = TypeVar("_ADBDeviceT", bound="AndroidTVEntity")
_R = TypeVar("_R")
_P = ParamSpec("_P")
type _FuncType[_T, **_P, _R] = Callable[Concatenate[_T, _P], Awaitable[_R]]
type _ReturnFuncType[_T, **_P, _R] = Callable[
Concatenate[_T, _P], Coroutine[Any, Any, _R | None]
]

_FuncType = Callable[Concatenate[_ADBDeviceT, _P], Awaitable[_R]]
_ReturnFuncType = Callable[Concatenate[_ADBDeviceT, _P], Coroutine[Any, Any, _R | None]]


def adb_decorator(
def adb_decorator[_ADBDeviceT: AndroidTVEntity, **_P, _R](
override_available: bool = False,
) -> Callable[[_FuncType[_ADBDeviceT, _P, _R]], _ReturnFuncType[_ADBDeviceT, _P, _R]]:
"""Wrap ADB methods and catch exceptions.
Expand Down
12 changes: 4 additions & 8 deletions homeassistant/components/asuswrt/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from collections.abc import Awaitable, Callable, Coroutine
import functools
import logging
from typing import Any, TypeVar, cast
from typing import Any, cast

from aioasuswrt.asuswrt import AsusWrt as AsusWrtLegacy
from aiohttp import ClientSession
Expand Down Expand Up @@ -56,15 +56,11 @@

_LOGGER = logging.getLogger(__name__)

type _FuncType[_T] = Callable[[_T], Awaitable[list[Any] | tuple[Any] | dict[str, Any]]]
type _ReturnFuncType[_T] = Callable[[_T], Coroutine[Any, Any, dict[str, Any]]]

_AsusWrtBridgeT = TypeVar("_AsusWrtBridgeT", bound="AsusWrtBridge")
_FuncType = Callable[
[_AsusWrtBridgeT], Awaitable[list[Any] | tuple[Any] | dict[str, Any]]
]
_ReturnFuncType = Callable[[_AsusWrtBridgeT], Coroutine[Any, Any, dict[str, Any]]]


def handle_errors_and_zip(
def handle_errors_and_zip[_AsusWrtBridgeT: AsusWrtBridge](
exceptions: type[Exception] | tuple[type[Exception], ...], keys: list[str] | None
) -> Callable[[_FuncType[_AsusWrtBridgeT]], _ReturnFuncType[_AsusWrtBridgeT]]:
"""Run library methods and zip results or manage exceptions."""
Expand Down
14 changes: 4 additions & 10 deletions homeassistant/components/cast/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from functools import wraps
import json
import logging
from typing import TYPE_CHECKING, Any, Concatenate, ParamSpec, TypeVar
from typing import TYPE_CHECKING, Any, Concatenate

import pychromecast
from pychromecast.controllers.homeassistant import HomeAssistantController
Expand Down Expand Up @@ -85,18 +85,12 @@

CAST_SPLASH = "https://www.home-assistant.io/images/cast/splash.png"

type _FuncType[_T, **_P, _R] = Callable[Concatenate[_T, _P], _R]

_CastDeviceT = TypeVar("_CastDeviceT", bound="CastDevice")
_R = TypeVar("_R")
_P = ParamSpec("_P")

_FuncType = Callable[Concatenate[_CastDeviceT, _P], _R]
_ReturnFuncType = Callable[Concatenate[_CastDeviceT, _P], _R]


def api_error(
def api_error[_CastDeviceT: CastDevice, **_P, _R](
func: _FuncType[_CastDeviceT, _P, _R],
) -> _ReturnFuncType[_CastDeviceT, _P, _R]:
) -> _FuncType[_CastDeviceT, _P, _R]:
"""Handle PyChromecastError and reraise a HomeAssistantError."""

@wraps(func)
Expand Down
14 changes: 6 additions & 8 deletions homeassistant/components/hassio/addon_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from enum import Enum
from functools import partial, wraps
import logging
from typing import Any, Concatenate, ParamSpec, TypeVar
from typing import Any, Concatenate

from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
Expand All @@ -28,15 +28,13 @@
async_update_addon,
)

_AddonManagerT = TypeVar("_AddonManagerT", bound="AddonManager")
_R = TypeVar("_R")
_P = ParamSpec("_P")
type _FuncType[_T, **_P, _R] = Callable[Concatenate[_T, _P], Awaitable[_R]]
type _ReturnFuncType[_T, **_P, _R] = Callable[
Concatenate[_T, _P], Coroutine[Any, Any, _R]
]

_FuncType = Callable[Concatenate[_AddonManagerT, _P], Awaitable[_R]]
_ReturnFuncType = Callable[Concatenate[_AddonManagerT, _P], Coroutine[Any, Any, _R]]


def api_error(
def api_error[_AddonManagerT: AddonManager, **_P, _R](
error_message: str,
) -> Callable[
[_FuncType[_AddonManagerT, _P, _R]], _ReturnFuncType[_AddonManagerT, _P, _R]
Expand Down
12 changes: 6 additions & 6 deletions homeassistant/components/heos/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from functools import reduce, wraps
import logging
from operator import ior
from typing import Any, ParamSpec
from typing import Any

from pyheos import HeosError, const as heos_const

Expand Down Expand Up @@ -41,8 +41,6 @@
SIGNAL_HEOS_UPDATED,
)

_P = ParamSpec("_P")

BASE_SUPPORTED_FEATURES = (
MediaPlayerEntityFeature.VOLUME_MUTE
| MediaPlayerEntityFeature.VOLUME_SET
Expand Down Expand Up @@ -90,11 +88,13 @@ async def async_setup_entry(
async_add_entities(devices, True)


_FuncType = Callable[_P, Awaitable[Any]]
_ReturnFuncType = Callable[_P, Coroutine[Any, Any, None]]
type _FuncType[**_P] = Callable[_P, Awaitable[Any]]
type _ReturnFuncType[**_P] = Callable[_P, Coroutine[Any, Any, None]]


def log_command_error(command: str) -> Callable[[_FuncType[_P]], _ReturnFuncType[_P]]:
def log_command_error[**_P](
command: str,
) -> Callable[[_FuncType[_P]], _ReturnFuncType[_P]]:
"""Return decorator that logs command failure."""

def decorator(func: _FuncType[_P]) -> _ReturnFuncType[_P]:
Expand Down
28 changes: 19 additions & 9 deletions homeassistant/components/http/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from collections.abc import Callable, Coroutine
from functools import wraps
from typing import Any, Concatenate, ParamSpec, TypeVar, overload
from typing import Any, Concatenate, overload

from aiohttp.web import Request, Response, StreamResponse

Expand All @@ -13,16 +13,18 @@

from .view import HomeAssistantView

_HomeAssistantViewT = TypeVar("_HomeAssistantViewT", bound=HomeAssistantView)
_ResponseT = TypeVar("_ResponseT", bound=Response | StreamResponse)
_P = ParamSpec("_P")
_FuncType = Callable[
Concatenate[_HomeAssistantViewT, Request, _P], Coroutine[Any, Any, _ResponseT]
type _ResponseType = Response | StreamResponse
type _FuncType[_T, **_P, _R] = Callable[
Concatenate[_T, Request, _P], Coroutine[Any, Any, _R]
]


@overload
def require_admin(
def require_admin[
_HomeAssistantViewT: HomeAssistantView,
**_P,
_ResponseT: _ResponseType,
](
_func: None = None,
*,
error: Unauthorized | None = None,
Expand All @@ -33,12 +35,20 @@ def require_admin(


@overload
def require_admin(
def require_admin[
_HomeAssistantViewT: HomeAssistantView,
**_P,
_ResponseT: _ResponseType,
](
_func: _FuncType[_HomeAssistantViewT, _P, _ResponseT],
) -> _FuncType[_HomeAssistantViewT, _P, _ResponseT]: ...


def require_admin(
def require_admin[
_HomeAssistantViewT: HomeAssistantView,
**_P,
_ResponseT: _ResponseType,
](
_func: _FuncType[_HomeAssistantViewT, _P, _ResponseT] | None = None,
*,
error: Unauthorized | None = None,
Expand Down
10 changes: 3 additions & 7 deletions homeassistant/components/izone/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from collections.abc import Callable, Mapping
import logging
from typing import Any, Concatenate, ParamSpec, TypeVar
from typing import Any, Concatenate

from pizone import Controller, Zone
import voluptuous as vol
Expand Down Expand Up @@ -48,11 +48,7 @@
IZONE,
)

_DeviceT = TypeVar("_DeviceT", bound="ControllerDevice | ZoneDevice")
_T = TypeVar("_T")
_R = TypeVar("_R")
_P = ParamSpec("_P")
_FuncType = Callable[Concatenate[_T, _P], _R]
type _FuncType[_T, **_P, _R] = Callable[Concatenate[_T, _P], _R]

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -119,7 +115,7 @@ def init_controller(ctrl: Controller):
)


def _return_on_connection_error(
def _return_on_connection_error[_DeviceT: ControllerDevice | ZoneDevice, **_P, _R, _T](
ret: _T = None, # type: ignore[assignment]
) -> Callable[[_FuncType[_DeviceT, _P, _R]], _FuncType[_DeviceT, _P, _R | _T]]:
def wrap(func: _FuncType[_DeviceT, _P, _R]) -> _FuncType[_DeviceT, _P, _R | _T]:
Expand Down