-
-
Notifications
You must be signed in to change notification settings - Fork 34.3k
Improve warnings on undefined template errors #48713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve warnings on undefined template errors #48713
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' 10000 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we set it to None
after the render?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about that. Is there any scenario in which we could print a stale ContextVar?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's mainly for garbage collection purposes.
@@ -1314,12 +1319,59 @@ def urlencode(value): | |||
return urllib_urlencode(value).encode("utf-8") | |||
|
|||
|
|||
def _render_with_context( | |||
template_str: str, template: jinja2.Template, **kwargs: Any |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.yaml
Additional 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: