Make TypeVars private (2) (#68206)
This commit is contained in:
parent
be7ef6115c
commit
eae0c75620
5 changed files with 31 additions and 29 deletions
|
@ -9,9 +9,9 @@ from typing import TypeVar, cast
|
|||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.loader import bind_hass
|
||||
|
||||
T = TypeVar("T")
|
||||
_T = TypeVar("_T")
|
||||
|
||||
FUNC = Callable[[HomeAssistant], T]
|
||||
FUNC = Callable[[HomeAssistant], _T]
|
||||
|
||||
|
||||
def singleton(data_key: str) -> Callable[[FUNC], FUNC]:
|
||||
|
@ -26,30 +26,30 @@ def singleton(data_key: str) -> Callable[[FUNC], FUNC]:
|
|||
|
||||
@bind_hass
|
||||
@functools.wraps(func)
|
||||
def wrapped(hass: HomeAssistant) -> T:
|
||||
def wrapped(hass: HomeAssistant) -> _T:
|
||||
if data_key not in hass.data:
|
||||
hass.data[data_key] = func(hass)
|
||||
return cast(T, hass.data[data_key])
|
||||
return cast(_T, hass.data[data_key])
|
||||
|
||||
return wrapped
|
||||
|
||||
@bind_hass
|
||||
@functools.wraps(func)
|
||||
async def async_wrapped(hass: HomeAssistant) -> T:
|
||||
async def async_wrapped(hass: HomeAssistant) -> _T:
|
||||
if data_key not in hass.data:
|
||||
evt = hass.data[data_key] = asyncio.Event()
|
||||
result = await func(hass)
|
||||
hass.data[data_key] = result
|
||||
evt.set()
|
||||
return cast(T, result)
|
||||
return cast(_T, result)
|
||||
|
||||
obj_or_evt = hass.data[data_key]
|
||||
|
||||
if isinstance(obj_or_evt, asyncio.Event):
|
||||
await obj_or_evt.wait()
|
||||
return cast(T, hass.data[data_key])
|
||||
return cast(_T, hass.data[data_key])
|
||||
|
||||
return cast(T, obj_or_evt)
|
||||
return cast(_T, obj_or_evt)
|
||||
|
||||
return async_wrapped
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue