Improve singleton helper typing (#75461)
* Improve singleton helper typing * Fix type errorspull/75536/head
parent
6da25c733e
commit
ac858cc2b5
|
@ -25,6 +25,7 @@ homeassistant.helpers.entity_values
|
||||||
homeassistant.helpers.event
|
homeassistant.helpers.event
|
||||||
homeassistant.helpers.reload
|
homeassistant.helpers.reload
|
||||||
homeassistant.helpers.script_variables
|
homeassistant.helpers.script_variables
|
||||||
|
homeassistant.helpers.singleton
|
||||||
homeassistant.helpers.sun
|
homeassistant.helpers.sun
|
||||||
homeassistant.helpers.translation
|
homeassistant.helpers.translation
|
||||||
homeassistant.util.async_
|
homeassistant.util.async_
|
||||||
|
|
|
@ -305,9 +305,7 @@ class RestoreEntity(Entity):
|
||||||
# Return None if this entity isn't added to hass yet
|
# Return None if this entity isn't added to hass yet
|
||||||
_LOGGER.warning("Cannot get last state. Entity not added to hass") # type: ignore[unreachable]
|
_LOGGER.warning("Cannot get last state. Entity not added to hass") # type: ignore[unreachable]
|
||||||
return None
|
return None
|
||||||
data = cast(
|
data = await RestoreStateData.async_get_instance(self.hass)
|
||||||
RestoreStateData, await RestoreStateData.async_get_instance(self.hass)
|
|
||||||
)
|
|
||||||
if self.entity_id not in data.last_states:
|
if self.entity_id not in data.last_states:
|
||||||
return None
|
return None
|
||||||
return data.last_states[self.entity_id]
|
return data.last_states[self.entity_id]
|
||||||
|
|
|
@ -4,23 +4,23 @@ from __future__ import annotations
|
||||||
import asyncio
|
import asyncio
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
import functools
|
import functools
|
||||||
from typing import TypeVar, cast
|
from typing import Any, TypeVar, cast
|
||||||
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.loader import bind_hass
|
from homeassistant.loader import bind_hass
|
||||||
|
|
||||||
_T = TypeVar("_T")
|
_T = TypeVar("_T")
|
||||||
|
|
||||||
FUNC = Callable[[HomeAssistant], _T]
|
_FuncType = Callable[[HomeAssistant], _T]
|
||||||
|
|
||||||
|
|
||||||
def singleton(data_key: str) -> Callable[[FUNC], FUNC]:
|
def singleton(data_key: str) -> Callable[[_FuncType[_T]], _FuncType[_T]]:
|
||||||
"""Decorate a function that should be called once per instance.
|
"""Decorate a function that should be called once per instance.
|
||||||
|
|
||||||
Result will be cached and simultaneous calls will be handled.
|
Result will be cached and simultaneous calls will be handled.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def wrapper(func: FUNC) -> FUNC:
|
def wrapper(func: _FuncType[_T]) -> _FuncType[_T]:
|
||||||
"""Wrap a function with caching logic."""
|
"""Wrap a function with caching logic."""
|
||||||
if not asyncio.iscoroutinefunction(func):
|
if not asyncio.iscoroutinefunction(func):
|
||||||
|
|
||||||
|
@ -35,10 +35,10 @@ def singleton(data_key: str) -> Callable[[FUNC], FUNC]:
|
||||||
|
|
||||||
@bind_hass
|
@bind_hass
|
||||||
@functools.wraps(func)
|
@functools.wraps(func)
|
||||||
async def async_wrapped(hass: HomeAssistant) -> _T:
|
async def async_wrapped(hass: HomeAssistant) -> Any:
|
||||||
if data_key not in hass.data:
|
if data_key not in hass.data:
|
||||||
evt = hass.data[data_key] = asyncio.Event()
|
evt = hass.data[data_key] = asyncio.Event()
|
||||||
result = await func(hass)
|
result = await func(hass) # type: ignore[misc]
|
||||||
hass.data[data_key] = result
|
hass.data[data_key] = result
|
||||||
evt.set()
|
evt.set()
|
||||||
return cast(_T, result)
|
return cast(_T, result)
|
||||||
|
@ -51,6 +51,6 @@ def singleton(data_key: str) -> Callable[[FUNC], FUNC]:
|
||||||
|
|
||||||
return cast(_T, obj_or_evt)
|
return cast(_T, obj_or_evt)
|
||||||
|
|
||||||
return async_wrapped
|
return async_wrapped # type: ignore[return-value]
|
||||||
|
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
3
mypy.ini
3
mypy.ini
|
@ -87,6 +87,9 @@ disallow_any_generics = true
|
||||||
[mypy-homeassistant.helpers.script_variables]
|
[mypy-homeassistant.helpers.script_variables]
|
||||||
disallow_any_generics = true
|
disallow_any_generics = true
|
||||||
|
|
||||||
|
[mypy-homeassistant.helpers.singleton]
|
||||||
|
disallow_any_generics = true
|
||||||
|
|
||||||
[mypy-homeassistant.helpers.sun]
|
[mypy-homeassistant.helpers.sun]
|
||||||
disallow_any_generics = true
|
disallow_any_generics = true
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue