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

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

Open
chris-chambers opened this issue Mar 17, 2022 · 0 comments
Open

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

chris-chambers opened this issue Mar 17, 2022 · 0 comments

Comments

@chris-chambers
Copy link

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
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