From e386bab682019c0c69691221478e3cbc37f6c249 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 24 Nov 2022 15:30:21 +0100 Subject: [PATCH] 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> --- homeassistant/helpers/template.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index 2b191d7537a..8d284f042c2 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -29,6 +29,7 @@ import jinja2 from jinja2 import pass_context, pass_environment, pass_eval_context from jinja2.sandbox import ImmutableSandboxedEnvironment from jinja2.utils import Namespace +from typing_extensions import Concatenate, ParamSpec import voluptuous as vol from homeassistant.const import ( @@ -96,6 +97,8 @@ _COLLECTABLE_STATE_ATTRIBUTES = { } _T = TypeVar("_T") +_R = TypeVar("_R") +_P = ParamSpec("_P") ALL_STATES_RATE_LIMIT = timedelta(minutes=1) DOMAIN_STATES_RATE_LIMIT = timedelta(seconds=1) @@ -2079,12 +2082,14 @@ class TemplateEnvironment(ImmutableSandboxedEnvironment): # evaluated fresh with every execution, rather than executed # at compile time and the value stored. The context itself # 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.""" @wraps(func) - def wrapper(*args, **kwargs): - return func(hass, *args[1:], **kwargs) + def wrapper(_: Any, *args: _P.args, **kwargs: _P.kwargs) -> _R: + return func(hass, *args, **kwargs) return pass_context(wrapper)