Skip to content

Commit 80eb4fb

Browse files
authored
Replace asyncio.iscoroutinefunction (home-assistant#148738)
1 parent 9e3a78b commit 80eb4fb

File tree

11 files changed

+22
-18
lines changed

11 files changed

+22
-18
lines changed

homeassistant/components/knx/websocket.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
from __future__ import annotations
44

5-
import asyncio
65
from collections.abc import Awaitable, Callable
76
from functools import wraps
7+
import inspect
88
from typing import TYPE_CHECKING, Any, Final, overload
99

1010
import knx_frontend as knx_panel
@@ -116,7 +116,7 @@ def _send_not_loaded_error(
116116
"KNX integration not loaded.",
117117
)
118118

119-
if asyncio.iscoroutinefunction(func):
119+
if inspect.iscoroutinefunction(func):
120120

121121
@wraps(func)
122122
async def with_knx(

homeassistant/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ def get_hassjob_callable_job_type(target: Callable[..., Any]) -> HassJobType:
384384
while isinstance(check_target, functools.partial):
385385
check_target = check_target.func
386386

387-
if asyncio.iscoroutinefunction(check_target):
387+
if inspect.iscoroutinefunction(check_target):
388388
return HassJobType.Coroutinefunction
389389
if is_callback(check_target):
390390
return HassJobType.Callback

homeassistant/helpers/condition.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
from __future__ import annotations
44

55
import abc
6-
import asyncio
76
from collections import deque
87
from collections.abc import Callable, Container, Coroutine, Generator, Iterable
98
from contextlib import contextmanager
109
from datetime import datetime, time as dt_time, timedelta
1110
import functools as ft
11+
import inspect
1212
import logging
1313
import re
1414
import sys
@@ -359,7 +359,7 @@ def disabled_condition(
359359
while isinstance(check_factory, ft.partial):
360360
check_factory = check_factory.func
361361

362-
if asyncio.iscoroutinefunction(check_factory):
362+
if inspect.iscoroutinefunction(check_factory):
363363
return cast(ConditionCheckerType, await factory(hass, config))
364364
return cast(ConditionCheckerType, factory(config))
365365

homeassistant/helpers/frame.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
from __future__ import annotations
44

5-
import asyncio
65
from collections.abc import Callable
76
from dataclasses import dataclass
87
import enum
98
import functools
9+
import inspect
1010
import linecache
1111
import logging
1212
import sys
@@ -397,7 +397,7 @@ def _report_usage_no_integration(
397397

398398
def warn_use[_CallableT: Callable](func: _CallableT, what: str) -> _CallableT:
399399
"""Mock a function to warn when it was about to be used."""
400-
if asyncio.iscoroutinefunction(func):
400+
if inspect.iscoroutinefunction(func):
401401

402402
@functools.wraps(func)
403403
async def report_use(*args: Any, **kwargs: Any) -> None:

homeassistant/helpers/http.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
from __future__ import annotations
44

5-
import asyncio
65
from collections.abc import Awaitable, Callable
76
from contextvars import ContextVar
87
from http import HTTPStatus
8+
import inspect
99
import logging
1010
from typing import Any, Final
1111

@@ -45,7 +45,7 @@ def request_handler_factory(
4545
hass: HomeAssistant, view: HomeAssistantView, handler: Callable
4646
) -> Callable[[web.Request], Awaitable[web.StreamResponse]]:
4747
"""Wrap the handler classes."""
48-
is_coroutinefunction = asyncio.iscoroutinefunction(handler)
48+
is_coroutinefunction = inspect.iscoroutinefunction(handler)
4949
assert is_coroutinefunction or is_callback(handler), (
5050
"Handler should be a coroutine or a callback."
5151
)

homeassistant/helpers/service.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import dataclasses
88
from enum import Enum
99
from functools import cache, partial
10+
import inspect
1011
import logging
1112
from types import ModuleType
1213
from typing import TYPE_CHECKING, Any, TypedDict, cast, override
@@ -997,7 +998,7 @@ def decorator(
997998
service_handler: Callable[[ServiceCall], Any],
998999
) -> Callable[[ServiceCall], Any]:
9991000
"""Decorate."""
1000-
if not asyncio.iscoroutinefunction(service_handler):
1001+
if not inspect.iscoroutinefunction(service_handler):
10011002
raise HomeAssistantError("Can only decorate async functions.")
10021003

10031004
async def check_permissions(call: ServiceCall) -> Any:

homeassistant/helpers/singleton.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import asyncio
66
from collections.abc import Callable, Coroutine
77
import functools
8+
import inspect
89
from typing import Any, Literal, assert_type, cast, overload
910

1011
from homeassistant.core import HomeAssistant
@@ -47,7 +48,7 @@ def wrapper(func: _FuncType[_U]) -> _FuncType[_U]: ...
4748

4849
def wrapper(func: _FuncType[_Coro[_T] | _U]) -> _FuncType[_Coro[_T] | _U]:
4950
"""Wrap a function with caching logic."""
50-
if not asyncio.iscoroutinefunction(func):
51+
if not inspect.iscoroutinefunction(func):
5152

5253
@functools.lru_cache(maxsize=1)
5354
@bind_hass

homeassistant/helpers/trigger.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from collections.abc import Callable, Coroutine, Iterable
99
from dataclasses import dataclass, field
1010
import functools
11+
import inspect
1112
import logging
1213
from typing import TYPE_CHECKING, Any, Protocol, TypedDict, cast
1314

@@ -407,7 +408,7 @@ def _trigger_action_wrapper(
407408
check_func = check_func.func
408409

409410
wrapper_func: Callable[..., Any] | Callable[..., Coroutine[Any, Any, Any]]
410-
if asyncio.iscoroutinefunction(check_func):
411+
if inspect.iscoroutinefunction(check_func):
411412
async_action = cast(Callable[..., Coroutine[Any, Any, Any]], action)
412413

413414
@functools.wraps(async_action)

homeassistant/util/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
from __future__ import annotations
44

5-
import asyncio
65
from collections.abc import Callable, Coroutine, Iterable, KeysView, Mapping
76
from datetime import datetime, timedelta
87
from functools import wraps
8+
import inspect
99
import random
1010
import re
1111
import string
@@ -125,7 +125,7 @@ def __init__(
125125
def __call__(self, method: Callable) -> Callable:
126126
"""Caller for the throttle."""
127127
# Make sure we return a coroutine if the method is async.
128-
if asyncio.iscoroutinefunction(method):
128+
if inspect.iscoroutinefunction(method):
129129

130130
async def throttled_value() -> None:
131131
"""Stand-in function for when real func is being throttled."""

tests/components/music_assistant/common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
import asyncio
5+
import inspect
66
from typing import Any
77
from unittest.mock import AsyncMock, MagicMock
88

@@ -191,7 +191,7 @@ async def trigger_subscription_callback(
191191
object_id=object_id,
192192
data=data,
193193
)
194-
if asyncio.iscoroutinefunction(cb_func):
194+
if inspect.iscoroutinefunction(cb_func):
195195
await cb_func(event)
196196
else:
197197
cb_func(event)

0 commit comments

Comments
 (0)