Skip to content

Commit

Permalink
Pass context to test when using select (#1762)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism authored Dec 20, 2024
2 parents 7a41ddb + d05bd38 commit 028f61d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Unreleased
contain the templates directory. :issue:`1705`
- Improve annotations for methods returning copies. :pr:`1880`
- ``urlize`` does not add ``mailto:`` to values like `@a@b`. :pr:`1870`
- Tests decorated with `@pass_context`` can be used with the ``|select``
filter. :issue:`1624`


Version 3.1.4
Expand Down
2 changes: 1 addition & 1 deletion src/jinja2/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1780,7 +1780,7 @@ def transfunc(x: V) -> V:
args = args[1 + off :]

def func(item: t.Any) -> t.Any:
return context.environment.call_test(name, item, args, kwargs)
return context.environment.call_test(name, item, args, kwargs, context)

except LookupError:
func = bool # type: ignore
Expand Down
12 changes: 12 additions & 0 deletions tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,18 @@ def test_nested_loop_scoping(self, env):
)
assert tmpl.render() == "hellohellohello"

def test_pass_context_with_select(self, env):
@pass_context
def is_foo(ctx, s):
assert ctx is not None
return s == "foo"

env.tests["foo"] = is_foo
tmpl = env.from_string(
"{% for x in ['one', 'foo'] | select('foo') %}{{ x }}{% endfor %}"
)
assert tmpl.render() == "foo"


@pytest.mark.parametrize("unicode_char", ["\N{FORM FEED}", "\x85"])
def test_unicode_whitespace(env, unicode_char):
Expand Down

0 comments on commit 028f61d

Please sign in to comment.