Skip to content
Myr

Merge pull request #1303 from gibson042/gh-1279-custom-async-filters

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fix custom async filters

authored by David Lord · committed by GitHub · parents 27c6575, e1d0131 · Browse files · 3 files+39 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+39 added−4 removed

  • tests:+29 added−0 removed
  • other:+10 added−4 removed

3 files+39 added−4 removed

modified:src/jinja2/nodes.py+7 added-4 removed
1"""AST nodes generated by the parser for the compiler. Also provides2some node tree helper functions used by the parser and compiler in order3to normalize nodes.4"""5import inspect6import operator7from collections import deque8from typing import Any9from typing import Tuple as TupleType
639 unmodified lines
649650 if filter_ is None or getattr(filter_, "contextfilter", False) is True:651 raise Impossible()652652 # We cannot constant handle async filters, so we need to make sure653 # to not go down this path.654 if eval_ctx.environment.is_async and getattr(655 filter_, "asyncfiltervariant", False653 # We cannot constant handle async filters, so we need to make654 # sure to not go down this path. Account for both sync/async and655 # pure-async filters.656 if eval_ctx.environment.is_async and (657 getattr(filter_, "asyncfiltervariant", False)658 or inspect.iscoroutinefunction(filter_)659 ):660 raise Impossible()661662 args, kwargs = args_as_const(self, eval_ctx)
395 unmodified lines
modified:tests/test_asyncfilters.py+29 added
1 unmodified line
23import pytest45from jinja2 import Environment6from jinja2.asyncsupport import auto_aiter7from jinja2.utils import Markup8910async def make_aiter(iter):
211 unmodified lines
222 assert out == (223 "[[0, 1, 2, 3], [4, 5, 6], [7, 8, 9]]|"224 "[[0, 1, 2, 3], [4, 5, 6, 'X'], [7, 8, 9, 'X']]"225 )226227228def test_custom_async_filter(env_async):229 async def customfilter(val):230 return str(val)231232 env_async.filters["customfilter"] = customfilter233 tmpl = env_async.from_string("{{ 'static'|customfilter }} {{ arg|customfilter }}")234 out = tmpl.render(arg="dynamic")235 assert out == "static dynamic"236237238@mark_dualiter("items", lambda: range(10))239def test_custom_async_iteratable_filter(env_async, items):240 async def customfilter(iterable):241 items = []242 async for item in auto_aiter(iterable):243 items.append(str(item))244 if len(items) == 3:245 break246 return ",".join(items)247248 env_async.filters["customfilter"] = customfilter249 tmpl = env_async.from_string(250 "{{ items()|customfilter }} .. {{ [3, 4, 5, 6]|customfilter }}"251 )252 out = tmpl.render(items=items)253 assert out == "0,1,2 .. 3,4,5"
modified:CHANGES.rst+3 added
12 unmodified lines
13 :class:`~loaders.PackageLoader`. :issue:`1168`14- Fix a bug that caused imported macros to not have access to the15 current template's globals. :issue:`688`16- Add ability to ignore ``trim_blocks`` using ``+%}``. :issue:`1036`17- Fix a bug that caused custom async-only filters to fail with18 constant input. :issue:`1279`192021Version 2.11.222--------------2324Released 2020-04-13
768 unmodified lines