Repository: asottile/pyupgrade

Issue:
Remove necessary extra parens from coroutine yield expression
Currently this is happening, but it causes a `SyntaxError` -- the extra set of parens is necessary here:

```diff
 def run_tests():
     url = options.url + '/getCaseCount'
     control_ws = yield websocket_connect(url, None)
-    num_tests = int((yield control_ws.read_message()))
+    num_tests = int(yield control_ws.read_message())
```

Discussion:

Patch:
diff --git a/pyupgrade.py b/pyupgrade.py
index b67cbf7..b53cf3d 100644
--- a/pyupgrade.py
+++ b/pyupgrade.py
@@ -551,8 +551,8 @@ def _fix_extraneous_parens(tokens, i):
     depth = 1
     while depth:
         i += 1
-        # found comma at depth 1: this is a tuple
-        if depth == 1 and tokens[i].src == ',':
+        # found comma or yield at depth 1: this is a tuple / coroutine
+        if depth == 1 and tokens[i].src in {',', 'yield'}:
             return
         elif tokens[i].src in OPENING:
             depth += 1


Test patch:
diff --git a/tests/pyupgrade_test.py b/tests/pyupgrade_test.py
index 9ab97d3..be62291 100644
--- a/tests/pyupgrade_test.py
+++ b/tests/pyupgrade_test.py
@@ -513,6 +513,9 @@ def test_fix_octal_literal(s, expected):
         'print((\n))',
         # don't touch parenthesized generators
         'sum((block.code for block in blocks), [])',
+        # don't touch coroutine yields
+        'def f():\n'
+        '    x = int((yield 1))\n',
     ),
 )
 def test_fix_extra_parens_noop(s):
@@ -545,6 +548,15 @@ def test_fix_extra_parens_noop(s):
             '        "foo"\n'
             ')\n',
         ),
+        pytest.param(
+            'def f():\n'
+            '    x = int(((yield 1)))\n',
+
+            'def f():\n'
+            '    x = int((yield 1))\n',
+
+            id='extra parens on coroutines are instead reduced to 2',
+        ),
     ),
 )
 def test_fix_extra_parens(s, expected):