Skip to content
Myr

Await in async version of the map filter (#913)

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

authored by seaders · committed by Armin Ronacher · parent 6e08416 · Browse files · 3 files+11 added−2 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+11 added−2 removed

  • tests:+9 added−0 removed
  • other:+2 added−2 removed

3 files+11 added−2 removed

modified:jinja2/asyncfilters.py+2 added-2 removed
1from functools import wraps23from jinja2.asyncsupport import auto_aiter3from jinja2.asyncsupport import auto_aiter, auto_await4from jinja2 import filters567async def auto_to_seq(value):
100 unmodified lines
108async def do_map(*args, **kwargs):109 seq, func = filters.prepare_map(args, kwargs)110 if seq:111 async for item in auto_aiter(seq):112 yield func(item)112 yield await auto_await(func(item))113114115@asyncfiltervariant(filters.do_sum)116async def do_sum(environment, iterable, attribute=None, start=0):
30 unmodified lines
modified:tests/test_asyncfilters.py+5 added
171 unmodified lines
172 tmpl = env_async.from_string('{{ items()|map("int")|sum }}')173 assert tmpl.render(items=items) == '6'174175176def test_map_sum(env_async): # async map + async filter177 tmpl = env_async.from_string('{{ [[1,2], [3], [4,5,6]]|map("sum")|list }}')178 assert tmpl.render() == '[3, 3, 15]'179180181@mark_dualiter('users', make_users)182def test_attribute_map(env_async, users):183 tmpl = env_async.from_string('{{ users()|map(attribute="name")|join("|") }}')184 assert tmpl.render(users=users) == 'john|jane|mike'
48 unmodified lines
modified:tests/test_filters.py+4 added
542 unmodified lines
543 env = Environment()544 tmpl = env.from_string('{{ ["1", "2", "3"]|map("int")|sum }}')545 assert tmpl.render() == '6'546547 def test_map_sum(self, env):548 tmpl = env.from_string('{{ [[1,2], [3], [4,5,6]]|map("sum")|list }}')549 assert tmpl.render() == '[3, 3, 15]'550551 def test_attribute_map(self, env):552 class User(object):553 def __init__(self, name):554 self.name = name
113 unmodified lines