Improve warnings on undefined template errors#48713
Improve warnings on undefined template errors#48713balloob merged 3 commits intohome-assistant:devfrom
Conversation
|
ℹ️ EDIT: Looking more closely, it is the comparison that breaks it. This is existing behavior and correct. It works pretty well in most cases. I've pasted a piece of a larger, random template into this and got an additional exception. Template used: {% if brightness is defined %}
{{ brightness }}
{% elif percent > 0 %}
{{ max_brightness }}
{% else %}
{{
(
(
(max_brightness - min_brightness) * ((100 + percent) / 100)
) + min_brightness
) | round
}}
{% endif %} |
| template_cv.set(template_str) | ||
| return template.render(**kwargs) |
There was a problem hiding this comment.
Should we set it to None after the render?
There was a problem hiding this comment.
I was thinking about that. Is there any scenario in which we could print a stale ContextVar?
There was a problem hiding this comment.
It's mainly for garbage collection purposes.
|
|
||
|
|
||
| def _render_with_context( | ||
| template_str: str, template: jinja2.Template, **kwargs: Any |
There was a problem hiding this comment.
It took me a bit to realize, this is wrapping Jinja2 objects. Why don't we store our own template objects in it so we can access the template string too?
In the future we can then add "names" to templates, ie sensor.kitchen_temp - attribute name
Proposed change
Improve warnings for undefined variables in templates introduced in #48140
Example: rendering template
"{{ no_such_variable }}"will result in this error message:Also fix some incorrect type annotations.
Type of change
Example entry for
configuration.yaml:# Example configuration.yamlAdditional information
Checklist
black --fast homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all..coveragerc.The integration reached or maintains the following Integration Quality Scale:
To help with the load of incoming pull requests: