mirror of
https://github.com/home-assistant/core.git
synced 2025-09-04 04:11:37 +02:00
Fix zero-argument functions with as_function (#150062)
This commit is contained in:
@@ -2030,7 +2030,7 @@ def apply(value, fn, *args, **kwargs):
|
|||||||
def as_function(macro: jinja2.runtime.Macro) -> Callable[..., Any]:
|
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."""
|
"""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
|
return_value = None
|
||||||
|
|
||||||
def returns(value):
|
def returns(value):
|
||||||
@@ -2039,7 +2039,7 @@ def as_function(macro: jinja2.runtime.Macro) -> Callable[..., Any]:
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
# Call the callable with the value and other args
|
# Call the callable with the value and other args
|
||||||
macro(value, *args, **kwargs, returns=returns)
|
macro(*args, **kwargs, returns=returns)
|
||||||
return return_value
|
return return_value
|
||||||
|
|
||||||
# Remove "macro_" from the macro's name to avoid confusion in the wrapper's name
|
# Remove "macro_" from the macro's name to avoid confusion in the wrapper's name
|
||||||
|
@@ -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:
|
def test_logarithm(hass: HomeAssistant) -> None:
|
||||||
"""Test logarithm."""
|
"""Test logarithm."""
|
||||||
tests = [
|
tests = [
|
||||||
|
Reference in New Issue
Block a user