Use TypeVarTuple for add_job and run_job methods (#114122)

This commit is contained in:
Marc Mueller 2024-03-24 18:52:39 +01:00 committed by GitHub
parent 67ab49b825
commit d779333bef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 17 deletions

View File

@ -116,7 +116,8 @@ async def process_wrong_login(request: Request) -> None:
""" """
hass = request.app[KEY_HASS] hass = request.app[KEY_HASS]
remote_addr = ip_address(request.remote) # type: ignore[arg-type] assert request.remote
remote_addr = ip_address(request.remote)
remote_host = request.remote remote_host = request.remote
with suppress(herror): with suppress(herror):
remote_host, _, _ = await hass.async_add_executor_job( remote_host, _, _ = await hass.async_add_executor_job(

View File

@ -54,6 +54,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def _async_update_data_alert() -> CurrentPhenomenons: async def _async_update_data_alert() -> CurrentPhenomenons:
"""Fetch data from API endpoint.""" """Fetch data from API endpoint."""
assert isinstance(department, str)
return await hass.async_add_executor_job( return await hass.async_add_executor_job(
client.get_warning_current_phenomenoms, department, 0, True client.get_warning_current_phenomenoms, department, 0, True
) )

View File

@ -40,6 +40,7 @@ from typing import (
ParamSpec, ParamSpec,
Self, Self,
TypedDict, TypedDict,
TypeVarTuple,
cast, cast,
overload, overload,
) )
@ -135,6 +136,7 @@ _T = TypeVar("_T")
_R = TypeVar("_R") _R = TypeVar("_R")
_R_co = TypeVar("_R_co", covariant=True) _R_co = TypeVar("_R_co", covariant=True)
_P = ParamSpec("_P") _P = ParamSpec("_P")
_Ts = TypeVarTuple("_Ts")
# Internal; not helpers.typing.UNDEFINED due to circular dependency # Internal; not helpers.typing.UNDEFINED due to circular dependency
_UNDEF: dict[Any, Any] = {} _UNDEF: dict[Any, Any] = {}
_CallableT = TypeVar("_CallableT", bound=Callable[..., Any]) _CallableT = TypeVar("_CallableT", bound=Callable[..., Any])
@ -529,7 +531,7 @@ class HomeAssistant:
self.bus.async_fire(EVENT_HOMEASSISTANT_STARTED) self.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
def add_job( def add_job(
self, target: Callable[..., Any] | Coroutine[Any, Any, Any], *args: Any self, target: Callable[[*_Ts], Any] | Coroutine[Any, Any, Any], *args: *_Ts
) -> None: ) -> None:
"""Add a job to be executed by the event loop or by an executor. """Add a job to be executed by the event loop or by an executor.
@ -547,7 +549,7 @@ class HomeAssistant:
) )
return return
if TYPE_CHECKING: if TYPE_CHECKING:
target = cast(Callable[..., Any], target) target = cast(Callable[[*_Ts], Any], target)
self.loop.call_soon_threadsafe( self.loop.call_soon_threadsafe(
functools.partial( functools.partial(
self.async_add_hass_job, HassJob(target), *args, eager_start=True self.async_add_hass_job, HassJob(target), *args, eager_start=True
@ -558,8 +560,8 @@ class HomeAssistant:
@callback @callback
def async_add_job( def async_add_job(
self, self,
target: Callable[..., Coroutine[Any, Any, _R]], target: Callable[[*_Ts], Coroutine[Any, Any, _R]],
*args: Any, *args: *_Ts,
eager_start: bool = False, eager_start: bool = False,
) -> asyncio.Future[_R] | None: ) -> asyncio.Future[_R] | None:
... ...
@ -568,8 +570,8 @@ class HomeAssistant:
@callback @callback
def async_add_job( def async_add_job(
self, self,
target: Callable[..., Coroutine[Any, Any, _R] | _R], target: Callable[[*_Ts], Coroutine[Any, Any, _R] | _R],
*args: Any, *args: *_Ts,
eager_start: bool = False, eager_start: bool = False,
) -> asyncio.Future[_R] | None: ) -> asyncio.Future[_R] | None:
... ...
@ -587,8 +589,9 @@ class HomeAssistant:
@callback @callback
def async_add_job( def async_add_job(
self, self,
target: Callable[..., Coroutine[Any, Any, _R] | _R] | Coroutine[Any, Any, _R], target: Callable[[*_Ts], Coroutine[Any, Any, _R] | _R]
*args: Any, | Coroutine[Any, Any, _R],
*args: *_Ts,
eager_start: bool = False, eager_start: bool = False,
) -> asyncio.Future[_R] | None: ) -> asyncio.Future[_R] | None:
"""Add a job to be executed by the event loop or by an executor. """Add a job to be executed by the event loop or by an executor.
@ -623,7 +626,7 @@ class HomeAssistant:
# the type used for the cast. For history see: # the type used for the cast. For history see:
# https://github.com/home-assistant/core/pull/71960 # https://github.com/home-assistant/core/pull/71960
if TYPE_CHECKING: if TYPE_CHECKING:
target = cast(Callable[..., Coroutine[Any, Any, _R] | _R], target) target = cast(Callable[[*_Ts], Coroutine[Any, Any, _R] | _R], target)
return self.async_add_hass_job(HassJob(target), *args, eager_start=eager_start) return self.async_add_hass_job(HassJob(target), *args, eager_start=eager_start)
@overload @overload
@ -772,7 +775,7 @@ class HomeAssistant:
@callback @callback
def async_add_executor_job( def async_add_executor_job(
self, target: Callable[..., _T], *args: Any self, target: Callable[[*_Ts], _T], *args: *_Ts
) -> asyncio.Future[_T]: ) -> asyncio.Future[_T]:
"""Add an executor job from within the event loop.""" """Add an executor job from within the event loop."""
task = self.loop.run_in_executor(None, target, *args) task = self.loop.run_in_executor(None, target, *args)
@ -783,7 +786,7 @@ class HomeAssistant:
@callback @callback
def async_add_import_executor_job( def async_add_import_executor_job(
self, target: Callable[..., _T], *args: Any self, target: Callable[[*_Ts], _T], *args: *_Ts
) -> asyncio.Future[_T]: ) -> asyncio.Future[_T]:
"""Add an import executor job from within the event loop.""" """Add an import executor job from within the event loop."""
task = self.loop.run_in_executor(self.import_executor, target, *args) task = self.loop.run_in_executor(self.import_executor, target, *args)
@ -844,14 +847,14 @@ class HomeAssistant:
@overload @overload
@callback @callback
def async_run_job( def async_run_job(
self, target: Callable[..., Coroutine[Any, Any, _R]], *args: Any self, target: Callable[[*_Ts], Coroutine[Any, Any, _R]], *args: *_Ts
) -> asyncio.Future[_R] | None: ) -> asyncio.Future[_R] | None:
... ...
@overload @overload
@callback @callback
def async_run_job( def async_run_job(
self, target: Callable[..., Coroutine[Any, Any, _R] | _R], *args: Any self, target: Callable[[*_Ts], Coroutine[Any, Any, _R] | _R], *args: *_Ts
) -> asyncio.Future[_R] | None: ) -> asyncio.Future[_R] | None:
... ...
@ -865,8 +868,9 @@ class HomeAssistant:
@callback @callback
def async_run_job( def async_run_job(
self, self,
target: Callable[..., Coroutine[Any, Any, _R] | _R] | Coroutine[Any, Any, _R], target: Callable[[*_Ts], Coroutine[Any, Any, _R] | _R]
*args: Any, | Coroutine[Any, Any, _R],
*args: *_Ts,
) -> asyncio.Future[_R] | None: ) -> asyncio.Future[_R] | None:
"""Run a job from within the event loop. """Run a job from within the event loop.
@ -894,7 +898,7 @@ class HomeAssistant:
# the type used for the cast. For history see: # the type used for the cast. For history see:
# https://github.com/home-assistant/core/pull/71960 # https://github.com/home-assistant/core/pull/71960
if TYPE_CHECKING: if TYPE_CHECKING:
target = cast(Callable[..., Coroutine[Any, Any, _R] | _R], target) target = cast(Callable[[*_Ts], Coroutine[Any, Any, _R] | _R], target)
return self.async_run_hass_job(HassJob(target), *args) return self.async_run_hass_job(HassJob(target), *args)
def block_till_done(self) -> None: def block_till_done(self) -> None: