From 269f6be096c7ac2e763d780db5557f427a92637d Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Wed, 14 Feb 2024 21:25:21 +0100 Subject: [PATCH] Improve hassio decorator typing (#110545) * Improve hassio decorator typing * Fix typing --- homeassistant/components/hassio/handler.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/hassio/handler.py b/homeassistant/components/hassio/handler.py index 9381b7951d6..38762fbc648 100644 --- a/homeassistant/components/hassio/handler.py +++ b/homeassistant/components/hassio/handler.py @@ -2,11 +2,11 @@ from __future__ import annotations import asyncio -from collections.abc import Coroutine +from collections.abc import Callable, Coroutine from http import HTTPStatus import logging import os -from typing import Any +from typing import Any, ParamSpec import aiohttp from yarl import URL @@ -23,6 +23,8 @@ from homeassistant.loader import bind_hass from .const import ATTR_DISCOVERY, DOMAIN, X_HASS_SOURCE +_P = ParamSpec("_P") + _LOGGER = logging.getLogger(__name__) @@ -30,10 +32,12 @@ class HassioAPIError(RuntimeError): """Return if a API trow a error.""" -def _api_bool(funct): +def _api_bool( + funct: Callable[_P, Coroutine[Any, Any, dict[str, Any]]], +) -> Callable[_P, Coroutine[Any, Any, bool]]: """Return a boolean.""" - async def _wrapper(*argv, **kwargs): + async def _wrapper(*argv: _P.args, **kwargs: _P.kwargs) -> bool: """Wrap function.""" try: data = await funct(*argv, **kwargs) @@ -44,10 +48,12 @@ def _api_bool(funct): return _wrapper -def api_data(funct): +def api_data( + funct: Callable[_P, Coroutine[Any, Any, dict[str, Any]]], +) -> Callable[_P, Coroutine[Any, Any, Any]]: """Return data of an api.""" - async def _wrapper(*argv, **kwargs): + async def _wrapper(*argv: _P.args, **kwargs: _P.kwargs) -> Any: """Wrap function.""" data = await funct(*argv, **kwargs) if data["result"] == "ok": @@ -80,7 +86,7 @@ async def async_get_addon_store_info(hass: HomeAssistant, slug: str) -> dict: @bind_hass -async def async_update_diagnostics(hass: HomeAssistant, diagnostics: bool) -> dict: +async def async_update_diagnostics(hass: HomeAssistant, diagnostics: bool) -> bool: """Update Supervisor diagnostics toggle. The caller of the function should handle HassioAPIError. @@ -255,7 +261,7 @@ async def async_update_core( @bind_hass @_api_bool -async def async_apply_suggestion(hass: HomeAssistant, suggestion_uuid: str) -> bool: +async def async_apply_suggestion(hass: HomeAssistant, suggestion_uuid: str) -> dict: """Apply a suggestion from supervisor's resolution center. The caller of the function should handle HassioAPIError.