Repository: pallets/jinja (python)

Issue:
custom filters with async_true enabled giving 'coroutine' object is not subscriptable error
**Description**
When applying a custom (synchronous) filter to a passed in object in a for loop, we are getting a `TypeError: 'coroutine' object is not subscriptable` Error. It seems to be an extension of this issue: https://github.com/pallets/jinja/issues/1141


**Reproduction**
Very similar to the above issue, you can minimally reproduce like:

`{% for item in (a.b|add_two(5))[:1] %}{{ item }}{% endfor %}`
with a jinja environment of:
```
jinja_env = jinja2.Environment(enable_async=True)
jinja_env.filters.update({'add_two': lambda x, y: [i+y for i in x]})
```

rendering with `a=dict(b=[1,2,3])` will result in the `'coroutine' object is not subscriptable`

We tried removing the parenthesis around the method call:
`{% for item in a.b|add_two(5)[:1] %}{{ item }}{% endfor %}`
but that results in a different `jinja2.exceptions.TemplateSyntaxError: expected token 'end of statement block', got '['` error


**Environment**

- Python version: 3.9
- Jinja version: 3.0.3


Pull request description:
add parens around `auto_await` for filters and calls
The compiler doesn't add parentheses around the `await auto_await(...)` expression when visiting filters and tests. If a subscription is used after the call, the compiler will produce code that tries to subscript the coroutine instead of the awaited result. This adds surrounding parentheses, which matches other similar situations.

fixes #1573 

Patch:
diff --git a/CHANGES.rst b/CHANGES.rst
index 8c041f60..d67381b1 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -31,6 +31,8 @@ Unreleased
 -   Filters and tests decorated with ``@async_variant`` are pickleable.
     :pr:`1612`
 -   Add ``items`` filter. :issue:`1561`
+-   Subscriptions (``[0]``, etc.) can be used after filters, tests, and
+    calls when the environment is in async mode. :issue:`1573`
 
 
 Version 3.0.3
diff --git a/src/jinja2/compiler.py b/src/jinja2/compiler.py
index dfea00aa..3458095f 100644
--- a/src/jinja2/compiler.py
+++ b/src/jinja2/compiler.py
@@ -1755,7 +1755,7 @@ class CodeGenerator(NodeVisitor):
         self, node: t.Union[nodes.Filter, nodes.Test], frame: Frame, is_filter: bool
     ) -> t.Iterator[None]:
         if self.environment.is_async:
-            self.write("await auto_await(")
+            self.write("(await auto_await(")
 
         if is_filter:
             self.write(f"{self.filters[node.name]}(")
@@ -1790,7 +1790,7 @@ class CodeGenerator(NodeVisitor):
         self.write(")")
 
         if self.environment.is_async:
-            self.write(")")
+            self.write("))")
 
     @optimizeconst
     def visit_Filter(self, node: nodes.Filter, frame: Frame) -> None:
@@ -1842,7 +1842,7 @@ class CodeGenerator(NodeVisitor):
         self, node: nodes.Call, frame: Frame, forward_caller: bool = False
     ) -> None:
         if self.environment.is_async:
-            self.write("await auto_await(")
+            self.write("(await auto_await(")
         if self.environment.sandboxed:
             self.write("environment.call(context, ")
         else:
@@ -1858,7 +1858,7 @@ class CodeGenerator(NodeVisitor):
         self.signature(node, frame, extra_kwargs)
         self.write(")")
         if self.environment.is_async:
-            self.write(")")
+            self.write("))")
 
     def visit_Keyword(self, node: nodes.Keyword, frame: Frame) -> None:
         self.write(node.key + "=")


Test patch:
diff --git a/tests/test_async.py b/tests/test_async.py
index 635727e2..c9ba70c3 100644
--- a/tests/test_async.py
+++ b/tests/test_async.py
@@ -642,3 +642,19 @@ def test_native_list_async(async_native_env):
         assert rv == [0, 1, 2]
 
     asyncio.run(_test())
+
+
+def test_getitem_after_filter():
+    env = Environment(enable_async=True)
+    env.filters["add_each"] = lambda v, x: [i + x for i in v]
+    t = env.from_string("{{ (a|add_each(2))[1:] }}")
+    out = t.render(a=range(3))
+    assert out == "[3, 4]"
+
+
+def test_getitem_after_call():
+    env = Environment(enable_async=True)
+    env.globals["add_each"] = lambda v, x: [i + x for i in v]
+    t = env.from_string("{{ add_each(a, 2)[1:] }}")
+    out = t.render(a=range(3))
+    assert out == "[3, 4]"