Repository: asottile/pyupgrade

Issue:
py3-plus: AssertionError: del statement in function
pyupgrade 2.4.2

Minimal test case:

```py
def foo(bar):
    del bar
```

```
pyupgrade --py3-plus test.py
```

```
Traceback (most recent call last):
  File "./venv/bin/pyupgrade", line 8, in <module>
    sys.exit(main())
  File "/home/jon/venv/lib64/python3.8/site-packages/pyupgrade.py", line 2680, in main
    ret |= _fix_file(filename, args)
  File "/home/jon/venv/lib64/python3.8/site-packages/pyupgrade.py", line 2638, in _fix_file
    contents_text = _fix_py3_plus(contents_text, args.min_version)
  File "/home/jon/venv/lib64/python3.8/site-packages/pyupgrade.py", line 2002, in _fix_py3_plus
    visitor.visit(ast_obj)
  File "/usr/lib64/python3.8/ast.py", line 360, in visit
    return visitor(node)
  File "/home/jon/venv/lib64/python3.8/site-packages/pyupgrade.py", line 1705, in generic_visit
    super().generic_visit(node)
  File "/usr/lib64/python3.8/ast.py", line 368, in generic_visit
    self.visit(item)
  File "/usr/lib64/python3.8/ast.py", line 360, in visit
    return visitor(node)
  File "/home/jon/venv/lib64/python3.8/site-packages/pyupgrade.py", line 1418, in _visit_sync_func
    self._visit_func(node)
  File "/home/jon/venv/lib64/python3.8/site-packages/pyupgrade.py", line 1414, in _visit_func
    self.generic_visit(node)
  File "/home/jon/venv/lib64/python3.8/site-packages/pyupgrade.py", line 1705, in generic_visit
    super().generic_visit(node)
  File "/usr/lib64/python3.8/ast.py", line 368, in generic_visit
    self.visit(item)
  File "/usr/lib64/python3.8/ast.py", line 360, in visit
    return visitor(node)
  File "/home/jon/venv/lib64/python3.8/site-packages/pyupgrade.py", line 1705, in generic_visit
    super().generic_visit(node)
  File "/usr/lib64/python3.8/ast.py", line 368, in generic_visit
    self.visit(item)
  File "/usr/lib64/python3.8/ast.py", line 360, in visit
    return visitor(node)
  File "/home/jon/venv/lib64/python3.8/site-packages/pyupgrade.py", line 1454, in visit_Name
    raise AssertionError(node)
AssertionError: <_ast.Name object at 0x7f6b26fd0a90>
```

Encountered in real life code: https://github.com/andymccurdy/redis-py/blob/7c0a67a5cb4a20aea44c590837b0e79c9c09c510/tests/test_multiprocessing.py#L157-L159

Discussion:

Patch:
diff --git a/pyupgrade.py b/pyupgrade.py
index 8e3bda3..a49ef68 100644
--- a/pyupgrade.py
+++ b/pyupgrade.py
@@ -1448,7 +1448,7 @@ class FindPy3Plus(ast.NodeVisitor):
         if self._scope_stack:
             if isinstance(node.ctx, ast.Load):
                 self._scope_stack[-1].reads.add(node.id)
-            elif isinstance(node.ctx, ast.Store):
+            elif isinstance(node.ctx, (ast.Store, ast.Del)):
                 self._scope_stack[-1].writes.add(node.id)
             else:
                 raise AssertionError(node)


Test patch:
diff --git a/tests/yield_from_test.py b/tests/yield_from_test.py
index bda592a..5aa9c45 100644
--- a/tests/yield_from_test.py
+++ b/tests/yield_from_test.py
@@ -205,6 +205,11 @@ def test_fix_yield_from(s, expected):
             '    g()()\n',
             id='variable referenced after loop, but via nested function',
         ),
+        pytest.param(
+            'def f(x):\n'
+            '    del x\n',
+            id='regression with del ctx (#306)',
+        ),
     ),
 )
 def test_fix_yield_from_noop(s):