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

Cannot refer to a variable named self that was set in an enclosing context #2030

Open
mwchase opened this issue Oct 2, 2024 · 0 comments
Open

Comments

@mwchase
Copy link

mwchase commented Oct 2, 2024

I hit this bug in two different ways, and I don't know if they have separate fixes or not.

The easy reproduction is, even accounting for the lack of positional-only arguments, passing a variable named self to Template.render does not work.
The more involved reproduction is, if there is a variable named self in some context, (I encountered this in a macro, but presumably {% set self = "whatever" %} would also work), then unlike other names, it is not visible in an {% include "blah" %}.

import jinja2


ENVIRONMENT = jinja2.Environment(loader=jinja2.BaseLoader())

WORKING_TEMPLATE = ENVIRONMENT.from_string("{{ self_ }}")
BROKEN_TEMPLATE = ENVIRONMENT.from_string("{{ self }}")

print("Works:", WORKING_TEMPLATE.render({"self_": ":)"}))
print("Doesn't work:", BROKEN_TEMPLATE.render({"self": ":("}))

Output with Jinja2==3.1.4:

Works: :)
Doesn't work: <TemplateReference None>

Expected output:

Works: :)
Doesn't work: :(

Environment:

  • Python version: Python 3.12.3
  • Jinja version: 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