Improve sonos error decorator typing (#67199)

This commit is contained in:
Marc Mueller 2022-03-15 09:24:52 +01:00 committed by GitHub
parent 66f506557f
commit 7ab9e5cf0f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 8 deletions

View file

@ -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}",

View file

@ -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

View file

@ -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",