Skip to content
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

meta.find_undeclared_variables returning variables set in if blocks as of 3.1.5 #2069

Open
alext opened this issue Jan 10, 2025 · 1 comment

Comments

@alext
Copy link

alext commented Jan 10, 2025

I've observed a change in behaviour between 3.1.4 and 3.1.5 where meta.find_undeclared_variables is now returning variables that are set in all branches of an if statement.

For example with the following:

from jinja2 import Environment, meta

env = Environment()

template = """\
{% if control == 'something' %}
    {% set output = 1 %}
{% elif control == 'something else' %}
    {% set output = 2 %}
{% else %}
    {% set output = 3 %}
{% endif %}
{{ output }}
"""
result = meta.find_undeclared_variables(env.parse(template))
assert result == {'control'}

With jinja2 version 3.1.4, this runs without error. With jinja2 version 3.1.5, this errors becasue result contains both "control" and "output". I believe the 3.1.4 behaviour is correct in this instance.

Reading the changelog, I suspect that the fix for #1253 (PR #1665) looks likely to be the cause of this change.

Environment:

  • Python version: 3.10.12
  • Jinja version: 3.1.5
@alext
Copy link
Author

alext commented Jan 10, 2025

I've just tested using jinja2 3.1.5 with the changes from #1665 reverted, and this results in the same behaviour as 3.1.4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant