pallets / jinja

A very fast and expressive template engine.
https://jinja.palletsprojects.com
BSD 3-Clause "New" or "Revised" License
10.33k stars 1.62k forks source link

unique filter doesn't work when chain with map filter in async variant #1886

Closed nikierpan closed 1 year ago

nikierpan commented 1 year ago

Unique filter doesn't work when chain with map filter in async variant

Template that reproduces the bug:

{{ users | map(attribute="username", default="Anonymous") | unique | join(", ") }}

Render code:

text = await jinja2.Template(
       template, enable_async=True
).render_async()

Traceback:

File ".../lib/python3.11/site-packages/jinja2/environment.py", line 1321, in <listcomp>
    [n async for n in self.root_render_func(ctx)]  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<template>", line 4, in top-level template code
  File ".../lib/python3.11/site-packages/jinja2/async_utils.py", line 65, in auto_await
    return await t.cast("t.Awaitable[V]", value)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../lib/python3.11/site-packages/jinja2/filters.py", line 603, in do_join
    return sync_do_join(eval_ctx, await auto_to_list(value), d, attribute)
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../lib/python3.11/site-packages/jinja2/async_utils.py", line 84, in auto_to_list
    return [x async for x in auto_aiter(value)]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../lib/python3.11/site-packages/jinja2/async_utils.py", line 84, in <listcomp>
    return [x async for x in auto_aiter(value)]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../lib/python3.11/site-packages/jinja2/async_utils.py", line 77, in auto_aiter
    for item in t.cast("t.Iterable[V]", iterable):
  File ".../lib/python3.11/site-packages/jinja2/filters.py", line 437, in do_unique
    for item in value:
TypeError: 'async_generator' object is not iterable

Environment:

nikierpan commented 1 year ago

https://github.com/pallets/jinja/issues/1781