Repository: psf/black

Issue:
Preview crashes on await (yield)


**Describe the bug**
`black --preview` crashes on seeing `await (yield)`
This is certainly not good code or anything, but is code we have [in one of our test files](https://github.com/Zac-HD/flake8-trio/blob/ec04f75f6c58515f58316870272fab315b828a35/tests/eval_files/trio108.py#L31-L34) in flake8-trio. Non-preview black has no issues with it.

**To Reproduce**


For example, take this code:

```python
async def foo():
    await (yield)
```

And run it with these arguments:

```sh
$ black --preview myfile.py 
```

The resulting error is:

```
error: cannot format myfile.py: blib2to3.pytree.Node object expected; got blib2to3.pytree.Leaf

Oh no! 💥 💔 💥
1 file failed to reformat.
```

**Expected behavior**

Not crashing

**Environment**

<!-- Please complete the following information: -->
```sh
$ black --version
black, 22.12.0 (compiled: yes)
Python (CPython) 3.11.0
$ python --version
Python 3.11.0
$ uname -sro
Linux 6.1.1-arch1-1 GNU/Linux
```


Discussion:
Thanks! We'll fix this before the stable release.
Note this only repros with mypyc enabled. This is the stack trace (from `black -v`):

```
Traceback (most recent call last):
  File "src/black/__init__.py", line 770, in reformat_one
  File "src/black/__init__.py", line 810, in format_file_in_place
  File "src/black/__init__.py", line 926, in format_file_contents
  File "src/black/__init__.py", line 1073, in format_str
  File "src/black/__init__.py", line 1100, in _format_str_once
  File "src/black/nodes.py", line 169, in visit
  File "src/black/linegen.py", line 139, in visit_default
  File "src/black/nodes.py", line 175, in visit_default
  File "src/black/nodes.py", line 167, in visit
  File "src/black/linegen.py", line 265, in visit_async_stmt
  File "src/black/nodes.py", line 167, in visit
  File "src/black/linegen.py", line 222, in visit_suite
  File "src/black/linegen.py", line 139, in visit_default
  File "src/black/nodes.py", line 175, in visit_default
  File "src/black/nodes.py", line 167, in visit
  File "src/black/linegen.py", line 248, in visit_simple_stmt
  File "src/black/linegen.py", line 139, in visit_default
  File "src/black/nodes.py", line 175, in visit_default
  File "src/black/nodes.py", line 167, in visit
  File "src/black/linegen.py", line 294, in visit_power
  File "src/black/linegen.py", line 1075, in remove_await_parens
TypeError: blib2to3.pytree.Node object expected; got blib2to3.pytree.Leaf
```

I think I found the cause; it's a bad `cast`. I'll propose a fix.

Resolving patch:
diff --git a/src/black/linegen.py b/src/black/linegen.py
--- a/src/black/linegen.py
+++ b/src/black/linegen.py
@@ -1223,18 +1223,19 @@ def remove_await_parens(node: Node) -> None:
             # N.B. We've still removed any redundant nested brackets though :)
             opening_bracket = cast(Leaf, node.children[1].children[0])
             closing_bracket = cast(Leaf, node.children[1].children[-1])
-            bracket_contents = cast(Node, node.children[1].children[1])
-            if bracket_contents.type != syms.power:
-                ensure_visible(opening_bracket)
-                ensure_visible(closing_bracket)
-            elif (
-                bracket_contents.type == syms.power
-                and bracket_contents.children[0].type == token.AWAIT
-            ):
-                ensure_visible(opening_bracket)
-                ensure_visible(closing_bracket)
-                # If we are in a nested await then recurse down.
-                remove_await_parens(bracket_contents)
+            bracket_contents = node.children[1].children[1]
+            if isinstance(bracket_contents, Node):
+                if bracket_contents.type != syms.power:
+                    ensure_visible(opening_bracket)
+                    ensure_visible(closing_bracket)
+                elif (
+                    bracket_contents.type == syms.power
+                    and bracket_contents.children[0].type == token.AWAIT
+                ):
+                    ensure_visible(opening_bracket)
+                    ensure_visible(closing_bracket)
+                    # If we are in a nested await then recurse down.
+                    remove_await_parens(bracket_contents)
 
 
 def _maybe_wrap_cms_in_parens(


Test patch:
diff --git a/tests/data/preview/remove_await_parens.py b/tests/data/preview/remove_await_parens.py
--- a/tests/data/preview/remove_await_parens.py
+++ b/tests/data/preview/remove_await_parens.py
@@ -77,6 +77,9 @@ async def main():
 async def main():
     await (await (await (await (await (asyncio.sleep(1))))))
 
+async def main():
+    await (yield)
+
 # output
 import asyncio
 
@@ -167,3 +170,7 @@ async def main():
 
 async def main():
     await (await (await (await (await asyncio.sleep(1)))))
+
+
+async def main():
+    await (yield)