From 7ab9e5cf0f7783e1aafae3fe98702a292466653f Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Tue, 15 Mar 2022 09:24:52 +0100 Subject: [PATCH] Improve sonos error decorator typing (#67199) --- homeassistant/components/sonos/helpers.py | 29 +++++++++++++++++++---- mypy.ini | 3 --- script/hassfest/mypy_config.py | 1 - 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/sonos/helpers.py b/homeassistant/components/sonos/helpers.py index a11847d2b0c..3edf23f0c3c 100644 --- a/homeassistant/components/sonos/helpers.py +++ b/homeassistant/components/sonos/helpers.py @@ -3,7 +3,7 @@ from __future__ import annotations from collections.abc import Callable import logging -from typing import TYPE_CHECKING, Any, TypeVar +from typing import TYPE_CHECKING, Any, TypeVar, overload from soco import SoCo from soco.exceptions import SoCoException, SoCoUPnPException @@ -17,6 +17,7 @@ from .exception import SonosUpdateError if TYPE_CHECKING: from .entity import SonosEntity from .household_coordinator import SonosHouseholdCoordinator + from .media import SonosMedia from .speaker import SonosSpeaker UID_PREFIX = "RINCON_" @@ -24,11 +25,31 @@ UID_POSTFIX = "01400" _LOGGER = logging.getLogger(__name__) -_T = TypeVar("_T", bound="SonosSpeaker | SonosEntity | SonosHouseholdCoordinator") +_T = TypeVar( + "_T", bound="SonosSpeaker | SonosMedia | SonosEntity | SonosHouseholdCoordinator" +) _R = TypeVar("_R") _P = ParamSpec("_P") +@overload +def soco_error( + errorcodes: None = ..., +) -> Callable[ # type: ignore[misc] + [Callable[Concatenate[_T, _P], _R]], Callable[Concatenate[_T, _P], _R] +]: + ... + + +@overload +def soco_error( + errorcodes: list[str], +) -> Callable[ # type: ignore[misc] + [Callable[Concatenate[_T, _P], _R]], Callable[Concatenate[_T, _P], _R | None] +]: + ... + + def soco_error( errorcodes: list[str] | None = None, ) -> Callable[ # type: ignore[misc] @@ -43,7 +64,7 @@ def soco_error( def wrapper(self: _T, *args: _P.args, **kwargs: _P.kwargs) -> _R | None: """Wrap for all soco UPnP exception.""" - args_soco = next((arg for arg in args if isinstance(arg, SoCo)), None) + args_soco = next((arg for arg in args if isinstance(arg, SoCo)), None) # type: ignore[attr-defined] try: result = funct(self, *args, **kwargs) except (OSError, SoCoException, SoCoUPnPException) as err: @@ -61,7 +82,7 @@ def soco_error( message = f"Error calling {function} on {target}: {err}" raise SonosUpdateError(message) from err - dispatch_soco = args_soco or self.soco + dispatch_soco = args_soco or self.soco # type: ignore[union-attr] dispatcher_send( self.hass, f"{SONOS_SPEAKER_ACTIVITY}-{dispatch_soco.uid}", diff --git a/mypy.ini b/mypy.ini index bf98c7dbcc6..16bf8394eee 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2660,9 +2660,6 @@ ignore_errors = true [mypy-homeassistant.components.sonos.favorites] ignore_errors = true -[mypy-homeassistant.components.sonos.helpers] -ignore_errors = true - [mypy-homeassistant.components.sonos.media_browser] ignore_errors = true diff --git a/script/hassfest/mypy_config.py b/script/hassfest/mypy_config.py index 7e739cfb7d2..fcc03a35524 100644 --- a/script/hassfest/mypy_config.py +++ b/script/hassfest/mypy_config.py @@ -128,7 +128,6 @@ IGNORED_MODULES: Final[list[str]] = [ "homeassistant.components.sonos.diagnostics", "homeassistant.components.sonos.entity", "homeassistant.components.sonos.favorites", - "homeassistant.components.sonos.helpers", "homeassistant.components.sonos.media_browser", "homeassistant.components.sonos.media_player", "homeassistant.components.sonos.number",