mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 15:17:35 +00:00
Add type hint to template wrapper (#82563)
* Add type to template hassfunction decorator * Adjust to use EvalContext * Use runtime.Context * Use TypeVar for context * Use jinja2.runtime.Context * Reverse declarations * Use Any * Update homeassistant/helpers/template.py Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
This commit is contained in:
parent
d0390860fb
commit
e386bab682
@ -29,6 +29,7 @@ import jinja2
|
|||||||
from jinja2 import pass_context, pass_environment, pass_eval_context
|
from jinja2 import pass_context, pass_environment, pass_eval_context
|
||||||
from jinja2.sandbox import ImmutableSandboxedEnvironment
|
from jinja2.sandbox import ImmutableSandboxedEnvironment
|
||||||
from jinja2.utils import Namespace
|
from jinja2.utils import Namespace
|
||||||
|
from typing_extensions import Concatenate, ParamSpec
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
@ -96,6 +97,8 @@ _COLLECTABLE_STATE_ATTRIBUTES = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_T = TypeVar("_T")
|
_T = TypeVar("_T")
|
||||||
|
_R = TypeVar("_R")
|
||||||
|
_P = ParamSpec("_P")
|
||||||
|
|
||||||
ALL_STATES_RATE_LIMIT = timedelta(minutes=1)
|
ALL_STATES_RATE_LIMIT = timedelta(minutes=1)
|
||||||
DOMAIN_STATES_RATE_LIMIT = timedelta(seconds=1)
|
DOMAIN_STATES_RATE_LIMIT = timedelta(seconds=1)
|
||||||
@ -2079,12 +2082,14 @@ class TemplateEnvironment(ImmutableSandboxedEnvironment):
|
|||||||
# evaluated fresh with every execution, rather than executed
|
# evaluated fresh with every execution, rather than executed
|
||||||
# at compile time and the value stored. The context itself
|
# at compile time and the value stored. The context itself
|
||||||
# can be discarded, we only need to get at the hass object.
|
# can be discarded, we only need to get at the hass object.
|
||||||
def hassfunction(func):
|
def hassfunction(
|
||||||
|
func: Callable[Concatenate[HomeAssistant, _P], _R],
|
||||||
|
) -> Callable[Concatenate[Any, _P], _R]:
|
||||||
"""Wrap function that depend on hass."""
|
"""Wrap function that depend on hass."""
|
||||||
|
|
||||||
@wraps(func)
|
@wraps(func)
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(_: Any, *args: _P.args, **kwargs: _P.kwargs) -> _R:
|
||||||
return func(hass, *args[1:], **kwargs)
|
return func(hass, *args, **kwargs)
|
||||||
|
|
||||||
return pass_context(wrapper)
|
return pass_context(wrapper)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user