Skip to content

Commit 94bade0

Browse files
depollfrenck
authored andcommitted
Fix zero-argument functions with as_function (home-assistant#150062)
1 parent 855e8b0 commit 94bade0

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

homeassistant/helpers/template.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,7 +2030,7 @@ def apply(value, fn, *args, **kwargs):
20302030
def as_function(macro: jinja2.runtime.Macro) -> Callable[..., Any]:
20312031
"""Turn a macro with a 'returns' keyword argument into a function that returns what that argument is called with."""
20322032

2033-
def wrapper(value, *args, **kwargs):
2033+
def wrapper(*args, **kwargs):
20342034
return_value = None
20352035

20362036
def returns(value):
@@ -2039,7 +2039,7 @@ def returns(value):
20392039
return value
20402040

20412041
# Call the callable with the value and other args
2042-
macro(value, *args, **kwargs, returns=returns)
2042+
macro(*args, **kwargs, returns=returns)
20432043
return return_value
20442044

20452045
# Remove "macro_" from the macro's name to avoid confusion in the wrapper's name

tests/helpers/test_template.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,23 @@ def test_as_function(hass: HomeAssistant) -> None:
845845
)
846846

847847

848+
def test_as_function_no_arguments(hass: HomeAssistant) -> None:
849+
"""Test as_function with no arguments."""
850+
assert (
851+
template.Template(
852+
"""
853+
{%- macro macro_get_hello(returns) -%}
854+
{%- do returns("Hello") -%}
855+
{%- endmacro -%}
856+
{%- set get_hello = macro_get_hello | as_function -%}
857+
{{ get_hello() }}
858+
""",
859+
hass,
860+
).async_render()
861+
== "Hello"
862+
)
863+
864+
848865
def test_logarithm(hass: HomeAssistant) -> None:
849866
"""Test logarithm."""
850867
tests = [

0 commit comments

Comments
 (0)