Skip to content

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

Open
@mwchase

Description

@mwchase

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions