Fix zero-argument functions with as_function (#150062)

This commit is contained in:
David Poll
2025-08-06 06:20:03 -07:00
committed by GitHub
parent fa3ce62ae8
commit 2215777cfb
2 changed files with 19 additions and 2 deletions

View File

@@ -2030,7 +2030,7 @@ def apply(value, fn, *args, **kwargs):
def as_function(macro: jinja2.runtime.Macro) -> Callable[..., Any]:
"""Turn a macro with a 'returns' keyword argument into a function that returns what that argument is called with."""
def wrapper(value, *args, **kwargs):
def wrapper(*args, **kwargs):
return_value = None
def returns(value):
@@ -2039,7 +2039,7 @@ def as_function(macro: jinja2.runtime.Macro) -> Callable[..., Any]:
return value
# Call the callable with the value and other args
macro(value, *args, **kwargs, returns=returns)
macro(*args, **kwargs, returns=returns)
return return_value
# Remove "macro_" from the macro's name to avoid confusion in the wrapper's name

View File

@@ -845,6 +845,23 @@ def test_as_function(hass: HomeAssistant) -> None:
)
def test_as_function_no_arguments(hass: HomeAssistant) -> None:
"""Test as_function with no arguments."""
assert (
template.Template(
"""
{%- macro macro_get_hello(returns) -%}
{%- do returns("Hello") -%}
{%- endmacro -%}
{%- set get_hello = macro_get_hello | as_function -%}
{{ get_hello() }}
""",
hass,
).async_render()
== "Hello"
)
def test_logarithm(hass: HomeAssistant) -> None:
"""Test logarithm."""
tests = [