Skip to content

Can't use a test decorated with @pass_context in select #1624

@chris-chambers

Description

@chris-chambers

When used from select, tests that are decorated with @pass_context fail with the error:

jinja2.exceptions.TemplateRuntimeError: Attempted to invoke a context test without context.

For example:

from jinja2 import Environment, pass_context

def main():
    env = Environment()
    env.tests["foo"] = is_foo
    output = env.from_string("""

    works: {{ "foo" is foo }}

    {%- for x in ["one", "foo" ] | select("foo") %}
    fails: {{ x }}
    {%- endfor %}

    """).render({})
    print(output)


@pass_context
def is_foo(ctx, s):
    return s == "foo"


if __name__ == "__main__":
    main()

Environment:

  • Python version: 3.10
  • Jinja version: 3.0.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions